-2

如何在 LINQ 中选择属于 group by 的字段?

例如。:

var result =   from a in table
           where Date >= DataStart && Date <= DataStop
           group a by a.Field2 into b
           select new
               {
                   AA = b.Sum(x => x.Total),
                   BB = a.Field2
               };
4

1 回答 1

5

由于您是分组依据Field2,Field2是组的关键,所以您可以使用b.Key

var result =   from a in table
               where a.Date >= DataStart && a.Date <= DataStop
               group a by a.Field2 into b
               select new
               {
                   AA = b.Sum(x => x.Total),
                   BB = b.Key
               };
于 2013-10-23T09:04:12.650 回答