linq join()方法,左联接和分组
根据以下样本,我试图将两个表与分组一起加入。我需要做的是应用左加入。尝试了几次,但
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我设法获得了所需的结果,但分两个步骤。第一个查询左将加入父母和孩子的第二步,以问题中提到的方式进行分组。
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.