一尘不染

LINQ按多个字段分组-语法帮助

c#

例如2 以便按多列分组需要进行的更正是什么

例子1

var query = from cm in cust
            group cm by new { cm.Customer, cm.OrderDate } into cms
            select
            new 
            { Key1 = cms.Key.Customer,Key2=cms.Key.OrderDate,Count=cms.Count() };

示例2(不正确)

   var qry = 
   cust.GroupBy(p => p.Customer, q => q.OrderDate, (k1, k2, group) =>
   new { Key1 = k1, Key2 = k2, Count = group.Count() });

阅读 692

收藏
2020-05-19

共1个答案

一尘不染

在点表达式中使用与查询表达式中相同的匿名类型:

var qry = cust.GroupBy(cm => new { cm.Customer, cm.OrderDate }, 
             (key, group) => new { Key1 = key.Customer, Key2 = key.OrderDate, 
                                   Count = group.Count() });

(在真正的IDE中,我将(key, group)在该cm参数下排队,但随后它将包裹在SO中。)

2020-05-19