Left join using LINQ

前端 未结 5 1520
心在旅途
心在旅途 2021-01-12 05:31

Could someone give me an example of how to perform a left join operation using LINQ/lambda expressions?

5条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-12 05:42

    well i tried to reproduce the famous left join where b key is null and the result i got is this extension method (with a little imagination you can modify it to just make a left join):

        public static class extends
    {
        public static IEnumerable LefJoinBNull(this IEnumerable source, IEnumerable Target, Func key)
        {
            if (source == null)
                throw new ArgumentException("source is null");
    
            return from s in source
                   join j in Target on key.Invoke(s) equals key.Invoke(j) into gg
                   from i in gg.DefaultIfEmpty()
                   where i == null
                   select s;
        }
    }
    

提交回复
热议问题