EF CORE Group by 及Sum的使用例子
例子
var headerTask = _coreDbContext.Set<myUserModel>().Where(u => u.IsDisabeld == 0).Select(c => new
{
c.UserKind,
c.UserCode
}).GroupBy(c => c.UserKind, (k, g) => new
{
ErpUserNum = g.Sum(a => a.UserKind == 1 ? 1 : 0),
SaleUserNum = g.Sum(a => (a.UserKind == 2 || a.UserKind == 6 || a.UserKind == 18 || a.UserKind == 22) ? 1 : 0),
FeiYongUserNum = g.Sum(a => (a.UserKind == 4 || a.UserKind == 6 || a.UserKind == 18 || a.UserKind == 22) ? 1 : 0)
}).ToList();
var ErpUserNum = _coreDbContext.Set<myUserModel>().Where(t => t.IsDisabeld == 0).Sum((a => a.UserKind == 1 ? 1 : 0)).ToString() ;
var SaleUserNum = _coreDbContext.Set<myUserModel>().Where(t => t.IsDisabeld == 0).Sum((a => (a.UserKind == 2 || a.UserKind == 6 || a.UserKind == 18 || a.UserKind == 22) ? 1 : 0)).ToString();
var FeiYongUserNum = _coreDbContext.Set<myUserModel>().Where(t => t.IsDisabeld == 0).Sum((a => (a.UserKind == 4 || a.UserKind == 6 || a.UserKind == 18 || a.UserKind == 22) ? 1 : 0)).ToString();