Linq中的group by多表多字段,Sum求和
生活随笔
收集整理的這篇文章主要介紹了
Linq中的group by多表多字段,Sum求和
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Linq中的group by多表多字段,Sum求和 //Line to Sql 寫法
var data = (from a in Itemsgroup a by new { a.GroupId, a.Id } into b//orderby new ComparerItem() { GroupId = b.Key.GroupId, Id = b.Key.Id } descending
.where(o => o.Id>1000)select new{GroupId = b.Key.GroupId,Id = b.Key.Id,Count = b.Sum(c => c.Count),Weight = b.Sum(c => c.Weight)}).OrderBy(t => t.GroupId).ThenBy(t => t.Id);
var data = (from a in Itemsgroup a by new { a.GroupId, a.Id } into b//orderby new ComparerItem() { GroupId = b.Key.GroupId, Id = b.Key.Id } descending
.where(o => o.Id>1000)select new{GroupId = b.Key.GroupId,Id = b.Key.Id,Count = b.Sum(c => c.Count),Weight = b.Sum(c => c.Weight)}).OrderBy(t => t.GroupId).ThenBy(t => t.Id);
items 是一個包含4個字段(GroupId, Id, Count, Weight)的list.
效果,按GroupId,Id 分組 ,并統計Count字段和Weight字段
//labmda 寫法var data = items.GroupBy( t=> t.GroupBy,t=> t.Id).where( f=> f.Id>1000).Select( g=> new {GroupId = g.GroupId,Id = g.Id,Count = g.Count( ),SumWeight = g.Sum( x=>x.Weight)});?
//多表寫法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; } }?
from c in Customersjoin p in Purchaseson c.ID equals p.CustomerIDgroup p.Price by new{p.Date.Year,c.Name}into salesByYearorderby salesByYear.Key.Year descendingselect new{TotalPrice= salesByYear.Sum(),Year=salesByYear.Key.Year,Name=salesByYear.Key.Name}?
posted on 2017-04-07 11:45 szsunny 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/sxypeace/p/6677424.html
總結
以上是生活随笔為你收集整理的Linq中的group by多表多字段,Sum求和的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: truncate与delete的区别
- 下一篇: 帽子世界如何制作12级武器-12种武器入