【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();

结果:

 

posted @ 2016-12-19 23:06  dongzhou  阅读(1665)  评论(0编辑  收藏  举报