分组多字段求和
query.GroupBy(q => new { q.Year, q.Month }) .Select(q => new { Year = q.Key.Year, Month = q.Key.Month, BuildAmount = q.Sum(i => i.BuildAmount), RecAmount = q.Sum(i => i.RecAmount), Amount = q.Sum(i => i.Amount), RealAmount = q.Sum(i => i.RealAmount) });
不分组多字段求和(这样得到的就是对应字段的总的求和,其实还是利用了分组,不过给分组依据传个空,如果利用linq的话就是传个常数)
where.GroupBy(x => new { }).Select(q => new { sumWeight = q.Sum(x => x.Weight), sumQuantity = q.Sum(x => x.Quantity), sumIncome = q.Sum(x => x.Income) }).FirstOrDefault();