Linq根据多字段group汇总个数并查询时间最晚的全部字段数据

IQueryable<Scan> query = UnitWork.Find<Scan>(null);
var scans = 
                from L in
                (
                    from gd in query
                    // 根据两个字段分组,视为左表
                    group gd by new { gd.ProductCode, gd.MaterialICode } into g
                    select new
                    {
                        // 分组查询时间最晚的一条数据
                        CreateTime = g.Max(m => m.CreateTime),
                        // 统计分组后的条数
                        Count = g.Count(),
                        // 查询分组最小id,删除使用,只删除分组中最早的一条数据
                        MinId = g.Min(m => m.Id),
                    }
                )
                join R in query on L.Id equals R.Id
                // 根据时间倒序
                orderby R.CreateTime descending
                join sup1 in UnitWork.Find<Supplier>(null)
                    on R.ProductSupplierCode equals sup1.SupplierCode into temp
                from t in temp.DefaultIfEmpty()
                join sup2 in UnitWork.Find<Supplier>(null)
                    on R.ComponentSupplierCode equals sup2.SupplierCode into temp1
                from o in temp1.DefaultIfEmpty()
                join sup3 in UnitWork.Find<Material>(null)
                    on R.ComponentMaterialCode equals sup3.MaterialCode into temp2
                from m in temp2.DefaultIfEmpty()
                select new
                {
                    R.Id,
                    R.ProductCode,
                    R.MaterialICode,
                    R.CreateId,
                    R.CreateTime,
                    R.ComponentMaterialCode,
                    ProductSupplierName = t.SupplierName,
                    ComponentSupplierName = o.SupplierName,
                    ComponentMaterialName = m.MaterialName,
                    Count = L.Count,
                    MinId = L.MinId
                };
            var scansViews = await scans.ToListAsync();

            return new TableData
            {
                count = scansViews.Count(),
                data = scansViews.OrderByDescending(u => u.Id)
                    .Skip((request.page - 1) * request.limit)
                    .Take(request.limit),
            };

此linq是扫码汇总项目中的查询片段,需要将成品码和部件码相同的数据合并,并展示出数据量,最晚扫码时间,调用删除时只删除时间最早的一条数据,

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值