想从IEnumerable<删除项目; T>采集 [英] Want to remove an item from a IEnumerable<T> collection
本文介绍了想从IEnumerable<删除项目; T>采集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个popuplated 的IEnumerable<使用者方式>
集
I have a popuplated IEnumerable<User>
collection.
我想从它删除一个项目,我该怎么做呢?
I want to remove an item from it, how can I do this?
foreach(var u in users)
{
if(u.userId = 1123)
{
// remove!
}
}
我知道你不是想而循环去除,所以我不介意或者创建一个新的集合或再将其取出。
I know your not suppose to remove while looping, so I don't mind either creating a new collection or removing it after.
但我不知道如何删除一个项目,有点失去了这方面的一些原因, !
But I don't know how to remove an item, kind of lost for some reason on this!
另外,我很困惑的也是,我怎么可以创建一个新的集合,如:
Alternately which I am confused on also, how can I create a new collection like:
IEnumerable<User> modifiedUsers = new List<User>();
foreach(var u in users)
{
if(u.userId != 1233)
{
modifiedUsers.add ??????
}
}
我如何添加到收藏?
How can I add to the collection?
推荐答案
使用LINQ:
// remove
users = users.Where(u => u.userId != 123).ToList();
// new list
var modified = users.Where(u => u.userId == 123).ToList();
这篇关于想从IEnumerable<删除项目; T>采集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文