在下面的查询中,很容易看出我们基本上是如何遍历sampleOrders并评估每个订单的,即可以访问ord的属性:
var o = (
from ord in sampleOrders
select ord.Quantity
在下面的查询中,我们将按产品分组的订单分为orderGroups。orderGroups从概念上讲是一个“列表列表”,其中每个列表都有一个键和一个包含关联订单的属性。前面的描述当然很松散-关键是,如果我迭代该集合,我希望会收到一个具有key属性和包含订单的属性的对象。此行为似乎与上面的第一个查询一致。但实际上每个组都是一个命令。通过迭代组列表来显示它,而我们绕过了该组本身,实际上是迭代了每个顺序。
为什么此分组行为与上面的第一个查询中的行为不同?
var o = (
from ord in sampleOrders
group ord by ord.Product into orderGroups
from grp in orderGroups
select grp // grp is an order, not a group of orders
顺便说一句,请注意,我不是在问如何访问组.....我的问题与概念和语法有关。这是访问每个组的方法。...我不是在问如何执行此操作:
var o = (
from ord in sampleOrders
group ord by ord.Product into orderGroups
select new {orderGroups.Key, SumQty=orderGroups.Sum(x => x.Quantity)}
编辑:每个请求的工作示例
public static void LinqTest()
{
List<Order> sampleOrders = new List<Order>()
{
new Order{Product="A", Quantity = 3},
new Order{Product="A", Quantity = 4},
new Order{Product="B", Quantity = 5},
new Order{Product="C", Quantity = 6},
};
var orders =
from ord in sampleOrders
group ord by ord.Product into orderGroups
select orderGroups;
foreach (var grp in orders) // here is the outer list
{
Console.WriteLine("Product: " + grp.Key.ToString());
// here is the inner list hence the term "list of lists"
// it is in fact a hierarchical object however this fact is irrelelvent to my question
//
foreach (Order ord in grp)
Console.WriteLine("Qty: " + ord.Quantity);
}
Console.WriteLine();
Console.WriteLine("following listing is grouped orders but we see each order not groups");
var orders2 =
from ord in sampleOrders
group ord by ord.Product into orderGroups
from grp in orderGroups
select grp;
foreach(Order ord2 in orders2)
Console.WriteLine("Product: " + ord2.Product); // each item returned is an order not a group of orders
Console.ReadKey();
}
}
class Order
{
public string Product { get; set; }
public int Quantity { get; set; }
}
编辑2:
这是澄清我的问题的另一种方法:在“ from listOfx中的x中”上方的第一个查询中,将为您提供一个对象x,该对象与listOfx组成的对象相同。
在组查询中,“来自listOfxGroups中的x”为您提供了一个与组成listOfxGroups的对象不同的对象。listOfxGroups实际上是一个组列表...但是,当我们对其进行迭代时,我们得到的x不是“ x的组”。
from ord in sampleOrders
group ord by ord.Product into orderGroups
from grp in orderGroups
select grp
乍一看,可能会认为这转化为:
sampleOrders.GroupBy(ord => ord.Product)
.Select(grp => grp); // grp is an `IGrouping<,>`
但实际上,它转换为:
sampleOrders.GroupBy(ord => ord.Product)
.SelectMany(orderGroup => orderGroup); // orderGroup is an `IGrouping<,>`
.Select(grp => grp); // grp is an Order
这就是LINQ语句的翻译方式:在初始from
关键字之后,所有剩余的from
s都会产生一个SelectMany。例如:
一个查询表达式,带有第二个from子句,后跟一个select子句
from x1 in e1 from x2 in e2 select v
被翻译成
( e1 ) . SelectMany( x1 => e2 , ( x1 , x2 ) => v )
C#5语言规范,第7.16.2.4节
from a in x
from b in a
select b
现在,in中的lambda表达式SelectMany
必须返回一个IEnumerable<T>
(它也这样做,因为IGrouping<TKey, TValue>
它也是一个IEnumerable<TValue>
),并且SelectMany将所有IEnumerable<T>
s展平为一个big IEnumerable<T>
,这意味着Order
在这种情况下,其中的单个项是s。
为了帮助澄清,请想象以下内容:
var orderGroups = // orderGroups is plural: IEnumerable<IGrouping<,>>
from ord in sampleOrders
group ord by ord.Product;
var o =
from orderGroup in orderGroups // orderGroup is singular: IGrouping<,>
from item in orderGroup // `item` instead of `grp`
select item;
实际上,这是使用into
子句生成的内容,但是具有不同的变量名。
var o =
from ord in sampleOrders
group ord by ord.Product into orderGroup
from item in orderGroup
select item; // item is an order, not a group of orders
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句