LINQ Join with Multiple From Clauses

前端 未结 4 1804
耶瑟儿~
耶瑟儿~ 2020-12-08 02:18

When writing LINQ queries in C#, I know I can perform a join using the join keyword. But what does the following do?

from c in Companies
from e          


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-08 02:27

    Multiple "from" statements are considered compound linq statments. They are like nested foreach statements. The msdn page does list a great example here

    var scoreQuery = from student in students
                     from score in student.Scores
                     where score > 90
                     select new { Last = student.LastName, score };
    

    this statement could be rewritten as:

    SomeDupCollection nameScore = new SomeDupCollection();
    foreach(Student curStudent in students)
    {
       foreach(Score curScore in curStudent.scores)
       {
          if (curScore > 90)
          {
             nameScore.Add(curStudent.LastName, curScore);
          }
       }
    }
    

提交回复
热议问题