使用LINQ选择组中的字段

时间:2013-10-23 09:02:22

标签: c# asp.net linq

如何在LINQ中选择属于分组的字段?

例如:

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
               };

1 个答案:

答案 0 :(得分: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
               };