asp.net core 3.1 efcore group by 使用方法

 //获取上下文
                        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

相关文章

数组的定义 Dim MyArray MyArray = Array(1‚5‚123‚12‚98...
\'参数: \'code:要检测的代码 \'leixing:html或者ubb \'n...
演示效果: 代码下载: 点击下载
环境:winxp sp2 ,mysql5.0.18,mysql odbc 3.51 driver 表采...
其实说起AJAX的初级应用是非常简单的,通俗的说就是客户端(j...
<% ’判断文件名是否合法 Function isFilename(aFilename...