LINQ Group By Multiple fields -Syntax help

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.)

Leave a Comment