在sql中,如果有group by,那么select的字段只能包含分组内容,或者count、sum、avg这些统计字段。
但在linq里面,是:group 你想要什么字段 by 分组字段
比如:
var q =
from p in db.Products
group p by p.CategoryID into g
select g;
实际应用中,多表多字段参与分组比较常见:
from a in TableA
join b in TableB on a.Id equals b.aId
where ((b.Type == || b.Type == || b.Type == ) && b.State == )
group new { a.Id, b.Name,b,CreateDate } by new { a.Id, b.Name } into g
select (new Class1 { Id = g.Key.Id, Name = g.Key.Name ?? "" }); class Class1
{
public int Id { get; set; }
publid string Name { get; set; }