Remove an item from an IEnumerable collection

后端 未结 9 1796
死守一世寂寞
死守一世寂寞 2020-12-09 07:30

I have a popuplated IEnumerable collection.

I want to remove an item from it, how can I do this?

foreach(var u in users)
{
          


        
9条回答
  •  轮回少年
    2020-12-09 07:49

    There is now an extension method to convert the IEnumerable<> to a Dictionary<,> which then has a Remove method.

    public readonly IEnumerable Users = new User[]; // or however this would be initialized
    
    // To take an item out of the collection
    Users.ToDictionary(u => u.Id).Remove(1123);
    
    // To take add an item to the collection
    Users.ToList().Add(newuser);
    

提交回复
热议问题