Remove an item from an IEnumerable collection

后端 未结 9 1811
死守一世寂寞
死守一世寂寞 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 08:07

    Not removing but creating a new List without that element with LINQ:

    // remove
    users = users.Where(u => u.userId != 123).ToList();
    
    // new list
    var modified = users.Where(u => u.userId == 123).ToList();
    

提交回复
热议问题