LINQ使用多个from子句联接 [英] LINQ Join with Multiple From Clauses

查看:77
本文介绍了LINQ使用多个from子句联接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用C#编写LINQ查询时,我知道可以使用join关键字执行联接.但是以下内容是做什么的?

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 in c.Employees
select e;

一本LINQ书,我曾说过这是一种联接类型,但不是正确的联接(使用join关键字).那么到底是什么类型的联接?

A LINQ book I have say it's a type of join, but not a proper join (which uses the join keyword). So exactly what type of join is it then?

推荐答案

多个"from"语句被视为复合linq语句.它们就像嵌套的foreach语句. msdn页面确实在此处

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 };

此语句可以重写为:

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

这篇关于LINQ使用多个from子句联接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆