如何.GroupBy多列LINQ / Projection?

时间:2011-11-25 22:08:28

标签: linq linq-to-sql

如何使用linq投影将多列分组?

这样的事情:

var q = db.Areas.GroupBy(x => x.AreaCatId, x.AreaCatName, x.AreaId, x.AreaName);

导致平面结果集,例如:

AreaCatId, AreaCatName, AreaId, AreaName
0          US           1       FL 
0          US           2       NY 
1          Canada       3       BC

1 个答案:

答案 0 :(得分:5)

您可以将GroupBy设为匿名类型:

var q = db.Areas.GroupBy(
            x => new 
                 {
                     CatId = x.AreaCatId, 
                     CatName = x.AreaCatName, 
                     Id = x.AreaId, 
                     Name = x.AreaName 
                 });