如何使用 linq 扩展方法执行左外连接 [英] How do you perform a left outer join using linq extension methods
本文介绍了如何使用 linq 扩展方法执行左外连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个左外连接:
Assuming I have a left outer join as such:
from f in Foo
join b in Bar on f.Foo_Id equals b.Foo_Id into g
from result in g.DefaultIfEmpty()
select new { Foo = f, Bar = result }
如何使用扩展方法表达相同的任务?例如
How would I express the same task using extension methods? E.g.
Foo.GroupJoin(Bar, f => f.Foo_Id, b => b.Foo_Id, (f,b) => ???)
.Select(???)
推荐答案
对于表 Bar
和 上的表
lambda 表示法:Foo
的(左外)联接Foo.Foo_Id = Bar.Foo_Id
For a (left outer) join of a table Bar
with a table Foo
on Foo.Foo_Id = Bar.Foo_Id
in lambda notation:
var qry = Foo.GroupJoin(
Bar,
foo => foo.Foo_Id,
bar => bar.Foo_Id,
(x,y) => new { Foo = x, Bars = y })
.SelectMany(
x => x.Bars.DefaultIfEmpty(),
(x,y) => new { Foo=x.Foo, Bar=y});
这篇关于如何使用 linq 扩展方法执行左外连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文