码迷,mamicode.com
首页 > 其他好文 > 详细

Linq中的group by多表多字段

时间:2018-12-28 11:36:13      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:sel   group by   date   class   HERE   分组   stat   rom   amp   

在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 == 1 || b.Type == 2 || b.Type == 5) && b.State == 1)
    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; }

 

Linq中的group by多表多字段

标签:sel   group by   date   class   HERE   分组   stat   rom   amp   

原文地址:https://www.cnblogs.com/jasonlai2016/p/10189416.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!