简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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