//获取上下文 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正常使用了。