通过分组拆分Linq的列表 [英] Splitting Linq List by grouping
问题描述
有关报告的目的我想拆分采购订单的名单到多个列表。一个列表每次购买地址。我知道这是可能的组列表通过购买地址,但我的问题是,如何通过这个购买地址列表分成多个列表,并使用这些多列表创建单独的报告文件。
for reporting purposes i wanna split a list of purchase orders into multiple lists. One list for each purchase address. I know it's possible to group the list by purchase address, but my question is how to split the list by this purchase address into multiple lists and use these multiple list to create individual reporting files.
代码:
(from o in orders
group o by new {field1, field2, field3, field4} into og
orderby og.Key.field1
select new ViewClass
{
purchaseAddress = og.Key.field1,
product = og.key.field2,
count = og.count
}).ToList()
问题:如何分割上面所列内容到每个purchaseAddress多个列表?
question: how to split above list into multiple lists for each purchaseAddress?
推荐答案
有一个内置的功能,我认为你想要做什么。如果我认为你的代码被分配到一个名为查询
变量,那么你可以这样写:
There's a built-in function that I think does what you want. If I assume that your code is assigned to a variable called query
then you can write this:
ILookup<string, ViewClass> queryLists = query.ToLookup(x => x.purchaseAddress);
这实质上创建您访问像一本字典列出的名单,除了每个值是一个列表零个或更多的值。是这样的:
This essentially creates a list of lists that you access like a dictionary, except that each value is a list of zero or more values. Something like:
IEnumerable<ViewClass> someList = queryLists["Some Address"];
这篇关于通过分组拆分Linq的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!