LINQ Group By Multiple字段 – 语法帮助

例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() }); 

在查询expression式中使用与点符号相同的匿名types:

 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中换行。)