LINQ ForEach可以if语句吗?

abc*_*cba 24 c# linq foreach if-statement list

是否可以if在LINQ ForEach调用中添加-statement ?

sequence.Where(x => x.Name.ToString().Equals("Apple"))
        .ToList()
        .ForEach( /* If statement here */ );
Run Code Online (Sandbox Code Playgroud)

Kei*_*las 48

你可以做以下......

List.Where(x => x.Name.ToString().Equals("Apple").ToList()
    .ForEach( x => { if(x.Name == ""){}} );
Run Code Online (Sandbox Code Playgroud)


tar*_*riq 33

是的,if语句通常在ForEach中使用,如下所示:

sequence.Where(x => x.Name.ToString().Equals("Apple"))
    .ToList()
    .ForEach( x =>
     {
       if(someCondition)
       {
         // Do some stuff here.
       }  
     });
Run Code Online (Sandbox Code Playgroud)