简体   繁体   English

Linq 具有左连接和分组的 Join() 方法

[英]Linq Join() method with left join and grouping

I am trying to join two tables with grouping as per following sample.我正在尝试按照以下示例通过分组连接两个表。 What i need to do is to apply Left join.我需要做的是应用左连接。 tried several times but failed尝试了几次但失败了

 var _result = _parents
                    .Join(_childs, p => p.PID, c => c.PID, ((p, c) => new { p, c }) )
                    .GroupBy(x => new { x.p.Category })
                    .Select(y =>
                    {
                        var _projs = y.Where(z => (z.p.Completed == false || z.p.Completed is null)).Select(z => z.p.Action);
                        var _actBys = y.Where(z => (z.p.Completed == false || z.p.Completed is null)).Select(z => z.pj.ActionBy);
                        var _nas = y.Where(z => (z.p.Completed == false || z.p.Completed is null)).Select(z => z.c.Action);

                        return new
                        {

                             Item= y.Key.Category,
                            Projects = string.Join(" + ", _projs.Distinct()),
                            ActionBys = string.Join(" + ", _actBys.Distinct()),
                            NextActions = string.Join(" + ", _nas),
                        };
                    });

If it is not possible to apply Left join on this query, is it possible to get the same query with applying left join using GroupJoin?如果无法在此查询上应用左连接,是否可以通过使用 GroupJoin 应用左连接来获得相同的查询?

I managed to get the required result but in two steps.我设法通过两个步骤获得了所需的结果。 First query left join Parents and Childs second step grouping in the way mentioned in the question.第一个query left join Parents and Childs 第二步按照问题中提到的方式分组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM