Linq实现InnerJoin&LeftJoin

public static class LinqEx
    {
        public static IEnumerable<TResult> LeftExcludingJoin<TSource, TInner, TKey, TResult>(this IEnumerable<TSource> source,
                                                    IEnumerable<TInner> inner,
                                                    Func<TSource, TKey> pk,
                                                    Func<TInner, TKey> fk,
                                                    Func<TSource, TInner, TResult> result)
            where TSource : class
            where TInner : class
        {
            IEnumerable<TResult> _result = Enumerable.Empty<TResult>();

            _result = from s in source
                      join i in inner
                      on pk(s) equals fk(i) into joinData
                      from left in joinData.DefaultIfEmpty()
                      where left == null
                      select result(s, left);

            return _result;
        }

        public static IEnumerable<TResult> LeftJoin<TSource, TInner, TKey, TResult>(this IEnumerable<TSource> source,
                                                    IEnumerable<TInner> inner,
                                                    Func<TSource, TKey> pk,
                                                    Func<TInner, TKey> fk,
                                                    Func<TSource, TInner, TResult> result)
            where TSource : class
            where TInner : class
        {
            IEnumerable<TResult> _result = Enumerable.Empty<TResult>();

            _result = from s in source
                      join i in inner
                      on pk(s) equals fk(i) into joinData
                      from left in joinData.DefaultIfEmpty()
                      select result(s, left);

            return _result;
        }

        public static IEnumerable<TResult> RightExcludingJoin<TSource, TInner, TKey, TResult>(this IEnumerable<TSource> source,
                                                        IEnumerable<TInner> inner,
                                                        Func<TSource, TKey> pk,
                                                        Func<TInner, TKey> fk,
                                                        Func<TSource, TInner, TResult> result)
            where TSource : class
            where TInner : class
        {
            IEnumerable<TResult> _result = Enumerable.Empty<TResult>();

            _result = from i in inner
                      join s in source
                      on fk(i) equals pk(s) into joinData
                      from right in joinData.DefaultIfEmpty()
                      where right == null
                      select result(right, i);

            return _result;
        }

        public static IEnumerable<TResult> FulltExcludingJoin<TSource, TInner, TKey, TResult>(this IEnumerable<TSource> source,
                                                            IEnumerable<TInner> inner,
                                                            Func<TSource, TKey> pk,
                                                            Func<TInner, TKey> fk,
                                                            Func<TSource, TInner, TResult> result)
            where TSource : class
            where TInner : class
        {
            var left = source.LeftJoin(inner, pk, fk, result).ToList();
            var right = source.RightExcludingJoin(inner, pk, fk, result).ToList();

            return left.Union(right);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TInner"></typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="source"></param>
        /// <param name="inner"></param>
        /// <param name="pk"></param>
        /// <param name="fk"></param>
        /// <param name="result"></param>
        /// <returns></returns>

        public static IEnumerable<TResult> InnerJoin<TSource, TInner, TKey, TResult>(this IEnumerable<TSource> source,
                                                    IEnumerable<TInner> inner,
                                                    Func<TSource, TKey> pk,
                                                    Func<TInner, TKey> fk,
                                                    Func<TSource, TInner, TResult> result)
            where TSource : class
            where TInner : class
        {
            IEnumerable<TResult> _result = Enumerable.Empty<TResult>();

            _result = from s in source
                      join i in inner
                      on pk(s) equals fk(i)
                      select result(s, i);

            return _result;
        }

    }


测试代码:

List<ProductOrder> orders = new List<ProductOrder>();
List<ProductOrderDetail> details = new List<ProductOrderDetail>();
orders.Add(new ProductOrder { OrderID = "2021", OrderDate = DateTime.Now });
orders.Add(new ProductOrder { OrderID = "2022", OrderDate = DateTime.Now });

details.Add(new ProductOrderDetail { OrderID = "2021", ProductCode = "A273197" });
details.Add(new ProductOrderDetail { OrderID = "2021", ProductCode = "A273198" });
details.Add(new ProductOrderDetail { OrderID = "2020", ProductCode = "A273196" });
details.Add(new ProductOrderDetail { OrderID = "2020", ProductCode = "A273195" });


var mm = orders.LeftJoin(details, x => x.OrderID, y => y.OrderID, (x, y) => new { x, y }).ToList();
var tt = orders.InnerJoin(details, x => x.OrderID, y => y.OrderID, (x, y) => new { x, y }).ToList();

上一篇:动态SQL-08


下一篇:10个经典场景带你玩转SQL优化