Linq中的group by多表多字段

原创
2018/01/29 18:31
阅读数 2.9K
1、单张表:
var q =  
    from p in db.Products  
    group p by p.CategoryID into g  
    select g;  

2、多表多字段参与分组:
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 MyClass { Id = g.Key.Id, Name = g.Key.Name ?? "" });  
  
class MyClass 
{  
    public int Id { get; set; }  
    publid string Name { get; set; }  
}    

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部