linq join()方法,左联接和分组

发布于 2025-01-19 19:28:07 字数 1157 浏览 2 评论 0原文

根据以下样本,我试图将两个表与分组一起加入。我需要做的是应用左加入。尝试了几次,但

 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),
                        };
                    });

如果无法在此查询中使用左JOIN,则失败了,是否可以使用GroupJoin使用左JON加入相同的查询?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

卷耳 2025-01-26 19:28:07

我设法获得了所需的结果,但分两个步骤。第一个查询左将加入父母和孩子的第二步,以问题中提到的方式进行分组。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文