动态GroupBy LINQ [复制]

问题描述 投票:1回答:1

这个问题在这里已有答案:

假设我有一堂课

public class Item
{ 
    public int Field1{get;set;}
    ……
    public int FieldN{get;set;}
}

我想像这样使用group by;

Collection.GroupBy(selector“Field1,…,FieldK”)
          .Select(x=> new Item
              {
                  Field1 = x.Key.Field1,
                   …
                  FieldK= x.Key.FieldK,
                  FiledK+1= x.Sum(x.FieldK+1),
                  ….
                  FiledN= x.Sum(x.FieldN)
              };

如何使用Expresions Tree执行此操作

c# linq group-by expression-trees
1个回答
0
投票

欢迎来到stackoverflow mahul!你想要的是这样的:

var a = from item in collection
        group item by new {item.Field1, ... , item.Fieldk} into newGroup
        select new {
            Field1 = newGroup.Key.Field1
            Field2 = newGroup.Key.Field1
            ...
            FieldK+1 = newGroup.Sum(x => x.FieldK+1),
            ...
            FieldN = newGroiup.Sum(x => x.FieldN)
         }

一定要阅读documentation。那里有很好的例子。

© www.soinside.com 2019 - 2024. All rights reserved.