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

为了按多列进行分组,需要对 例子2进行哪些校正

例子一

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

例二(不正确)

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

Use the same anonymous type in the dot notation that you do in the query expression:

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

(In a real IDE I'd have (key, group) lined up under the cm parameter, but then it would wrap in SO.)