我有一个列表,我想分解到另一个列表.
因此,当if语句正确时,必须将当前项添加到新列表中
var newList = oldList.ForEach( x =>
{
if (condition)
{
// select the current item
}
})
Run Code Online (Sandbox Code Playgroud)
选择当前项目的部分是问题
使用Where方法:
var newList = oldList.Where(x => condition(x));
Run Code Online (Sandbox Code Playgroud)
在此版本中,将对laList进行延迟评估.为了使结果具体,您可以另外评估结果:
var newList = oldList.Where(x => condition(x)).ToList();
Run Code Online (Sandbox Code Playgroud)