在下面的查询中,很容易看出我们如何基本循环遍历sampleOrders并评估每个订单,即我们可以访问ord的属性:
var o = (
from ord in sampleOrders
select ord.Quantity
在下面的查询中,我们按产品将订单分组到orderGroups。 orderGroups在概念上是一个"列表列表"其中每个列表都有一个键和一个包含相关订单的属性。前面的描述当然非常宽松 - 重点是,如果我迭代集合,我希望收到一个具有键属性和包含订单的属性的对象。此行为似乎与上面的第一个查询一致。但实际上每个小组都是一个订单。通过迭代我们绕过组本身的组列表,实际上迭代每个订单 为什么这种分组行为与上面第一个查询中的行为不同?
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:
这是另一个澄清我的问题: 在上面的第一个查询&#34;来自listOfx中的x&#34;给你一个对象x,它与listOfx组成的对象相同。
在群组查询中,&#34;来自listOfxGroups中的x&#34;为组件提供了一个与组成listOfxGroups的对象不同的对象。 listOfxGroups实际上是一个组列表...但是当我们迭代它时,我们得到的x不是&#34; x&#34;的组。
答案 0 :(得分:3)
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
都会生成SelectMany。例如:
带有第二个from子句后跟select子句
的查询表达式from x1 in e1 from x2 in e2 select v
被翻译成
( e1 ) . SelectMany( x1 => e2 , ( x1 , x2 ) => v )
C# 5 language spec,第7.16.2.4节
from a in x
from b in a
select b
现在,SelectMany
中的lambda表达式必须返回IEnumerable<T>
(它确实如此,因为IGrouping<TKey, TValue>
也是IEnumerable<TValue>
),并且SelectMany会使所有IEnumerable<T>
成为一个大IEnumerable<T>
,这意味着其中的各个项目都是Order
,在这种情况下。
为了帮助澄清,请想象以下内容:
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
答案 1 :(得分:2)
orderGroups在概念上是“列表列表”
不,不是。它在概念上是一个单个组,而不是一组组。当你说:
from grp in orderGroups
grp
现在代表组中的一个项目(在这种情况下是一个订单),而不是一个组,因为您正在迭代一个组中的所有项目。
如果您选择了该组,则然后整个查询将代表一个组列表(或列表列表)。
var listOfGroups = from order in sampleOrders
group order by order.Product into orderGroup
select orderGroup;
答案 2 :(得分:-1)
查看Microsoft的group caluse文档。
group子句返回一系列IGrouping对象,其中包含零个或多个与该组的键值匹配的项
所以如果你想要一个组返回你的代码可能如下所示:
var o = (
from ord in sampleOrders
group ord by ord.Product // Should return a group.
由于您使用的是into
关键字,因此您将使用“select
关键字来获取订单对象。