码迷,mamicode.com
首页 > Windows程序 > 详细

[转][C#]Linq 的扩展方法

时间:2018-12-30 17:30:21      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:ret   tin   list()   equal   col   lis   rabl   div   where   

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);
}
}

 

[转][C#]Linq 的扩展方法

标签:ret   tin   list()   equal   col   lis   rabl   div   where   

原文地址:https://www.cnblogs.com/z5337/p/10199817.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!