zoukankan      html  css  js  c++  java
  • [转][C#]Linq 的扩展方法

    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);
    }
    }
  • 相关阅读:
    云游四海
    保持良好的人际关系,赢得好人缘的八大诀窍
    二十三格经典的管理定律(建议收藏)
    游北湖公园有感
    如何成为领袖? 学习任正非小沃森郭士纳
    梦回江南
    观野花展有感
    爱一个人要爱多久
    醉卧山林
    游环岛路有感
  • 原文地址:https://www.cnblogs.com/z5337/p/10199817.html
Copyright © 2011-2022 走看看