[英]Linq inner join on complex query
var workbasketitems = new[] { //workbasketitems
new { TaskId = 10, WorkGroupId = 100, ActionUserId =1000, Work = "item 0" },
new { TaskId = 11, WorkGroupId = 101, ActionUserId =1001, Work = "item 1" },
new { TaskId = 12, WorkGroupId = 102, ActionUserId =1002, Work = "item 2" }
};
var workflowtasks = new[] { //workflowtasks
new { TaskId = 10, TaskDesc = "TaskDesc 0" },
new { TaskId = 11, TaskDesc = "TaskDesc 1" },
new { TaskId = 12, TaskDesc = "TaskDesc 2" }
};
var workgroup = new[] { //workgroup
new { WorkGroupId = 100, WGDesc = "WGDesc 0" },
new { WorkGroupId = 101, WGDesc = "WGDesc 1" },
new { WorkGroupId = 102, WGDesc = "WGDesc 2" }
};
var applicationuser = new[] { //applicationuser
new { AUId = 1000, AUDesc = "AUId 0" },
new { AUId = 1001, AUDesc = "AUId 1" }
};
var results = from wb in workbasketitems
join wft in workflowtasks on wb.TaskId equals wft.TaskId
join wg in workgroup on wb.WorkGroupId equals wg.WorkGroupId
select new { wft.TaskDesc, wg.WGDesc, wb.ActionUserId, wb.Work};
results.Dump();
var resultsInner = from wb in results
join au in applicationuser on wb.ActionUserId equals au.AUId into wbl
from auList in wbl.DefaultIfEmpty()
select new { wb.TaskDesc, wb.WGDesc, Desc = (auList == null? "BlAnk": auList.AUDesc), wb.Work};
resultsInner.Dump();
Is there a way to combine the below Linq queries, and is that helpful as I want keep things maintainable. 有没有一种方法可以组合下面的Linq查询,这对我保持可维护性很有帮助。 The above works in Linqpad with resultsInner being an Inner Join of the newly created results table.
上面的代码在Linqpad中工作,并且resultsInner是新创建的结果表的内部联接。
You are almost there. 你快到了。 Just add the extra
left join
that you are performing in the second query to the first query: 只需将您在第二个查询中执行的额外
left join
联接添加到第一个查询中:
var results = from wb in workbasketitems
join wft in workflowtasks on wb.TaskId equals wft.TaskId
join wg in workgroup on wb.WorkGroupId equals wg.WorkGroupId
join au in applicationuser on wb.ActionUserId equals au.AUId into wbl
from auList in wbl.DefaultIfEmpty()
select new { wft.TaskDesc, wg.WGDesc, Desc = (auList == null ? "BlAnk" : auList.AUDesc), wb.Work };
You can also use the other overload of DefaultIfEmpty
to specify what to do in a case when the left join
results with a null
: 您还可以使用
DefaultIfEmpty
的其他重载来指定在left join
结果为null
:
var results = from wb in workbasketitems
join wft in workflowtasks on wb.TaskId equals wft.TaskId
join wg in workgroup on wb.WorkGroupId equals wg.WorkGroupId
join au in applicationuser on wb.ActionUserId equals au.AUId into wbl
from auList in wbl.DefaultIfEmpty(new { AUId = wb.ActionUserId, AUDesc = "Blank" } )
select new { wft.TaskDesc, wg.WGDesc, Desc = auList.AUDesc, wb.Work };
Have you looking for this: 您在寻找这个吗?
var results = from wb in workbasketitems
join wft in workflowtasks on wb.TaskId equals wft.TaskId
join wg in workgroup on wb.WorkGroupId equals wg.WorkGroupId
join au in applicationuser on wb.ActionUserId equals au.AUId into wbl
from auList in wbl.DefaultIfEmpty()
select new { wft.TaskDesc, wg.WGDesc, Desc = (auList == null? "Blank": auList.AUDesc), wb.Work};
Related to maintainablity there is no impact. 与可维护性没有关系。 But in this case you have ommited extra projections.
但是在这种情况下,您将省略其他投影。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.