【发布时间】:2016-11-17 13:12:40
【问题描述】:
我有 2 张桌子:
specs {specId, desc, createdby, lastupdatedby}
用户 {userid, 用户名}
我希望下面的 linq 查询需要用纯 lambda 表达式编写
from spec in specs
from user in users.where(x => x.userId== spec.createdby).DefaultIfEmpty()
from updatedUser in users.where(x => x.userId== spec.lastupdatedbyby).DefaultIfEmpty()
select new {
spec = spec
user = user,
updatedUser = updatedUser
}
请帮忙。
数据就像说:
spec[{1, test, 1234, 2345},{2, test1, 1234, null}]
users[{1234, Allen},{2345, Dwayne}]
所以结果应该是
[{1, test, Allen, Dwayne}, {2, test1, Allen, null}]
【问题讨论】:
-
你能分享列表和你想要的结果吗?
-
@Eldeniz 我刚刚用示例数据和结果更新了我的问题。
-
@RustinCohle 请检查我的答案
-
@RustinCohle 不,它不支持左连接。我更新了答案:我花了一些时间解决这个问题,因为我从未遇到过这个问题。这是一个非常有趣的练习。请查看更新后的答案。
-
@RustinCohle 当然可以,请参阅我的更新答案。我列举了结果变量,但如果您愿意,您可以在 Linq 查询末尾的
ToList方法调用之后将ForEach方法调用加入队列