Is it possible to add if
-statement inside LINQ ForEach
call?
sequence.Where(x => x.Name.ToString().Equals("Apple"))
.ToList()
.ForEach( /* If statement here */ );
you can do the following...
List.Where(x => x.Name.ToString().Equals("Apple")).ToList()
.ForEach( x => { if(x.Name == ""){}} );
Yes, if-statement is commonly used inside the ForEach as below:
sequence.Where(x => x.Name.ToString().Equals("Apple"))
.ToList()
.ForEach( x =>
{
if(someCondition)
{
// Do some stuff here.
}
});
Yes, It takes a lambda expressions, so you can put any valid c# expression in there
Old thread but throwing an in my opinion cleaner syntax
foreach(var item in sequence.Where(s => s.Name.ToString() == "Apple"))
{
// do whatever
}
ForEach
method. That is a method ofList
.if
that can't be used as predicate in theWhere
?