我有一个列表,我想将其分解为另一个列表。
因此当 if 语句正确时,必须将当前项添加到新列表
var newList = oldList.ForEach( x =>
{
if (condition)
{
// select the current item
}
})
select the current item部分为题
最佳答案
使用Where :
var newList = oldList.Where(x => condition(x));
在此版本中,newList
将是一个带有 lazy 的对象评估。为了使结果具体化,您还可以立即对其进行评估:
var newList = oldList.Where(x => condition(x)).ToList();
关于c# - Linq 在 if 语句后选择项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18915318/