How to implement left join in JOIN Extension method

后端 未结 3 1746
时光说笑
时光说笑 2020-11-30 01:41

I am trying to implement an outer join on this kind of query for the p.Person table. How would I do this?

This example is taken from http://ashishware.c

3条回答
  •  庸人自扰
    2020-11-30 01:44

    If anyone comes across this question and wants an extension method to accomplish this, I created one using the same approach as the other answers. It has the same signature as the regular join extension method.

        public static IEnumerable LeftJoin(this IEnumerable outer,
            IEnumerable inner, Func outerKeySelector, Func innerKeySelector,
            Func resultSelector)
        {
            return outer
                .GroupJoin(inner, outerKeySelector, innerKeySelector, (outerObj, inners) =>
                new 
                {
                    outerObj,
                    inners= inners.DefaultIfEmpty()
                })
            .SelectMany(a => a.inners.Select(innerObj => resultSelector(a.outerObj, innerObj))); 
        }
    

提交回复
热议问题