C# C LINQ复合源vs.子查询

C# C LINQ复合源vs.子查询,c#,linq,subquery,C#,Linq,Subquery,在LINQ查询语法中,我可以有多个FROM 据我所知: 查询表达式可以包含子查询,子查询也可以以from子句开头 资料来源: 但是使用多个from的复合词和子查询有什么区别呢?来自的化合物示例如下: List<A> persons = new List<A>(); var query = from mychild in persons from ch in mychild.Children from c in ch.Ch

在LINQ查询语法中,我可以有多个FROM

据我所知:

查询表达式可以包含子查询,子查询也可以以from子句开头

资料来源:

但是使用多个from的复合词和子查询有什么区别呢?来自的化合物示例如下:

List<A> persons = new List<A>();

var query = from mychild in persons
            from ch in mychild.Children 
            from c in ch.Children 
            select c;
var query = from mychild in persons
            from ch in mychild.Children 
            from outerChild in 
                (from innerChild in ch.Children 
                select innerChild)
            select outerChild;

或者是从中国来的第二行。。mychild第一行的子查询和c第三行的子查询from ch?component from是所有子查询的名称?

component from子句是从外部查询访问内部元素时使用的。否则,可以定义在不同数据源上操作的子查询

考虑以下示例:

var somePersons = new List<Person>
{
    new Person { Names = new List<string> { "John", "Smith" } },
    new Person { Names = new List<string> { "Mike", "Soandso" } }
};

var otherPersons = new List<Person>
{
    new Person { Names = new List<string> { "Jane", "Doe" } },
    new Person { Names = new List<string> { "Sarah", "Connor" } }
};

var query = from person1 in somePersons
            from name1 in person1.Names
            from person2 in otherPersons
            from name2 in person2.Names
            select new Person { Names = new List<string> { name1, name2 } };

此结构转换为.SelectMany。它可用于向下导航1-many关系

var query =
  from customer in customers
  from order in customer.Orders
  select order;
此构造生成一个子查询

DateTime today = DateTime.Today;
var query =
  from customer in customers
  let orderCount = (
    from order in customer.Orders
    where order.ShipDate == today
    select order).Count()
  select new {customer, orderCount};

每个查询语法查询必须以select表达式结尾。这就是如何区分子查询的方法。如果你要解释:

from c in ch.Children 
select c;
作为子查询,则查询将是:

var query = from mychild in persons
            from ch in mychild.Children 
            (from c in ch.Children 
            select c);
以及外部查询:

from mychild in persons
from ch in mychild.Children 
将没有选择表达式

此外,如果这是一个子查询,它只会将查询粘贴到from语句的末尾,而不会描述如何使用它。要将其包含在外部查询中,需要以某种方式使用它。要使子查询成为子查询,可以执行以下操作:

List<A> persons = new List<A>();

var query = from mychild in persons
            from ch in mychild.Children 
            from c in ch.Children 
            select c;
var query = from mychild in persons
            from ch in mychild.Children 
            from outerChild in 
                (from innerChild in ch.Children 
                select innerChild)
            select outerChild;
显然,这不是一种有用的子查询方式,但这是将其解释为子查询所需的转换类型

DateTime today = DateTime.Today;
var query =
  from customer in customers
  let orderCount = (
    from order in customer.Orders
    where order.ShipDate == today
    select order).Count()
  select new {customer, orderCount};
所以,是的,只需计算select关键字的数量就可以看到查询的数量