asp.net core 3.1 efcore group by 使用方法
原创
©著作权归作者所有:来自51CTO博客作者沐雪编程人生的原创作品,请联系作者获取转载授权,否则将追究法律责任
//获取上下文
var recordContext = _dbContext.Set<tenant_couponrecord>();
var groupByCoupon = (from r in recordContext
where r.tenant_id == tenant_id && r.shop_code == shop_code && couponIdlist.Contains(r.CouponId)
group r by r.CouponId into gc
select new { CouponId = gc.Key, Count = gc.Count() }).ToList();
if (groupByCoupon != null && groupByCoupon.Count > 0)
{
foreach (var c in ret)
{
var groupC = groupByCoupon.Find(p => p.CouponId == c.Id);
if(groupC!=null)
{
c.RecoredCount = groupC.Count;
}
}
}
1、先获取该表/实体的 dbContext上下文;
2、写linq的lambda表达式;
3、表达式外面套一个ToList(),就可以像正常的List正常使用了。
更多可以学习ASP.NET Core 3.1官方教程 http://www.zyiz.net/xilie-293.html
作者:沐雪
文章均系作者原创或翻译,如有错误不妥之处,欢迎各位批评指正。本文版权归作者所有,如需转载恳请注明。
为之网-热爱软件编程 http://www.weizhi.cc/