LINQ - 由lambda表达式分组

时间:2012-09-08 13:11:30

标签: linq

说我有以下内容:

var OrderCounts = from o in Orders
                  group o by o.CustomerID into g
                  select new {
                         CustomerID = g.Key,
                         TotalOrders = g.Count()
                  };

如何将其转换为Lambda表达式

1 个答案:

答案 0 :(得分:9)

var OrderCounts = customers
        .GroupBy (o => o.CustomerID)
        .Select (o => new { CustomerID = o.Key, TotalOrders = o.Count () })