首页 > 其他 > 详细

Customer IEnuramble Extension

时间:2014-03-07 19:31:30      阅读:502      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
public static class IEnurambleExtension
{
    public static IEnumerable<TSource> DistinctBy<TSource, TKey>
        (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
    { 
        HashSet<TKey> keys = new HashSet<TKey>();

        foreach (TSource element in source)
            if (keys.Add(keySelector(element))) 
                yield return element;
    }

    public static IEnumerable<int> DistinctByReturnIndexes<TSource, TKey>
        (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
    {
        HashSet<TKey> keys = new HashSet<TKey>();
        int i = 0;
        foreach (TSource element in source)
        {
            if (!keys.Add(keySelector(element)))
                yield return i;
            i++;
        }
    }

    public static int FirstContainsWithIndex<TSource>
        (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
    {
        int i = 0;
        foreach (TSource element in source)
        {
            if (predicate(element))
                return i;
            i++;
        }

        return -1;
    }

    public static IEnumerable<int> ContainsReturnIndexes<TSource>
        (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
    {
        int i = 0;
        foreach (TSource element in source)
        {
            if (predicate(element))
               yield return i;
            i++;
        }
    }


    public static void Print<TSource, TKey>
         (this IEnumerable<TSource> source,  Func<TSource, TKey> keySelector)
        {
            foreach (TSource element in source)
                Console.WriteLine(keySelector(element));
        }
}
bubuko.com,布布扣

Customer IEnuramble Extension,布布扣,bubuko.com

Customer IEnuramble Extension

原文:http://www.cnblogs.com/yang_sy/p/3586764.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!