I\'d like to ask if it is possible to convert below nested foreach loops to LINQ expression.
public interface IFoo
{
bool IsCorrect(IFoo foo);
void DoSom
You can do this but this is not more efficient what you have so far:
var query= listA.SelectMany(a=>listB.Select(b=>new {a,b}))
.Where(e=>e.b.IsCorrect(e.a))
.ToList()// Foreach is a method of List
.Foreach(e=> e.b.DoSomething(e.a));
To avoid to call ToList, you can implement an extension method like this:
public static void ForEach(this System.Collection.Generic.IEnumerable list, System.Action action)
{
foreach (T item in list)
action(item);
}
And then your query would be:
var query= listA.SelectMany(a=>listB.Select(b=>new {a,b}))
.Where(e=>e.b.IsCorrect(e.a))
.Foreach(e=> e.b.DoSomething(e.a));