How to convert nested foreach loops with conditions to LINQ

前端 未结 4 824
小蘑菇
小蘑菇 2021-01-16 14:57

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         


        
4条回答
  •  难免孤独
    2021-01-16 15:43

    With method syntax you would use this query:

    var correctPairs = listA
        .SelectMany(a => listB.Where(b => b.IsCorrect(a)).Select(b => new { a, b }));
    
    foreach (var x in correctPairs)
        x.b.DoSomething(x.a);
    

提交回复
热议问题