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