【Linq to Object】使用LINQ实现左链接加GROUP BY查询

时间:2023-03-08 17:22:11
【Linq to Object】使用LINQ实现左链接加GROUP BY查询

直接上代码留记

var list = (from item in
(from tb1 in fileDirList
join tb2 in fileInfoList
on tb1.FileDirId equals tb2.FileDirId into tmp
from f in tmp.DefaultIfEmpty()
select new
{
FileCount = tmp.Where(w => w.FileDirId == f.FileDirId).Count(),
FileDirName = tb1.FileDirName,
NeedFiles = tb1.NeedFiles
})
group item by item.FileDirName into query
select new CheckFileCountDto
{
FileDirName = query.Max(m => m.FileDirName),
FileCount = query.Max(m => m.FileCount),
NeedFiles = query.Max(m => m.NeedFiles)
}).ToList();

结果:

【Linq to Object】使用LINQ实现左链接加GROUP BY查询