1. 程式人生 > >EF多字段求和(分組/不分組)

EF多字段求和(分組/不分組)

ear pre 字段 amount 就是 rec def nth clas

分組多字段求和

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();

EF多字段求和(分組/不分組)