How do you perform a left outer join using linq extension methods

前端 未结 7 1172
灰色年华
灰色年华 2020-11-22 10:12

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          


        
7条回答
  •  情歌与酒
    2020-11-22 10:57

    You can create extension method like:

    public static IEnumerable LeftOuterJoin(this IEnumerable source, IEnumerable other, Func func, Func innerkey, Func res)
        {
            return from f in source
                   join b in other on func.Invoke(f) equals innerkey.Invoke(b) into g
                   from result in g.DefaultIfEmpty()
                   select res.Invoke(f, result);
        }
    

提交回复
热议问题