如何.GroupBy多列LINQ/Projection?

gen*_*eek 4 linq linq-to-sql

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

像这样的东西:

var q = db.Areas.GroupBy(x => x.AreaCatId, x.AreaCatName, x.AreaId, x.AreaName);
Run Code Online (Sandbox Code Playgroud)

导致结果集平坦,例如:

AreaCatId, AreaCatName, AreaId, AreaName
0          US           1       FL 
0          US           2       NY 
1          Canada       3       BC
Run Code Online (Sandbox Code Playgroud)

Ree*_*sey 5

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

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