我有一个列表,其中包含n个列表(在这个例子中有4个)。我需要做的是获取输出,如下面的“必需查询结果”
列表清单:
Product Year DY
Comp 1992 110.0
Comp 1993 200.0
Non-Comp 1990 45.2
Non-Comp 1991 50.0
Non-Comp 1992 55.0
Non-Comp 1993 100.0
Product Year DY
Comp 1992 170.0
Non-Comp 1990 64.8
Non-Comp 1991 75.0
Non-Comp 1992 85.0
Product Year DY
Non-Comp 1991 25.0
Product Year DY
Non-Comp 1990 37.0
必填查询结果:
Product Year DY1 DY2 DY3 DY4
Comp 1992 110.0 170.0
Comp 1993 200.0
Non-Comp 1990 45.2 64.8 37.0
Non-Comp 1991 50.0 75.0 25.0
Non-Comp 1992 55.0 85.0
Non-Comp 1993 100.0
请记住,列表中可能有超过4个列表,所以我想象的是:
foreach (var element in myList)
{
// some hard work
}
答案 0 :(得分:1)
如果没有对输入/输出对象模型的更好描述,很难回答这个问题,但我想你想:
类似的东西:
var query = myListOfLists.SelectMany(list => list) // Flatten
.GroupBy(item => new { item.Product, item.Year }, // Group
item => item.DY); // Project
答案 1 :(得分:0)
答案 2 :(得分:0)
这样的东西?
List<List<Product>> nestedList;
List<Product> = allProducts = nestedList.SelectMany(x => x);
var groupped = allProducts
.GroupBy(x => new { Product = x.Product, Year = x.Year}, x => x.DY );