惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
Y
Y Combinator Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
M
MIT News - Artificial intelligence
GbyAI
GbyAI
A
About on SuperTechFans
T
The Blog of Author Tim Ferriss
雷峰网
雷峰网
Blog — PlanetScale
Blog — PlanetScale
J
Java Code Geeks
IT之家
IT之家
Microsoft Azure Blog
Microsoft Azure Blog
V
V2EX
爱范儿
爱范儿
N
Netflix TechBlog - Medium
U
Unit 42
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
博客园 - 叶小钗
G
Google Developers Blog
Jina AI
Jina AI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The GitHub Blog
The GitHub Blog
腾讯CDC

博客园 - On the road....

c# -- 对象销毁和垃圾回收 C#集合 -- Equality和Order插件 C#集合--Dictionary C#集合 -- 自定义集合与代理 C#集合 -- Lists,Queues, Stacks 和 Sets C#集合--ICollection接口和IList接口 C#集合--数组 C#集合-列举(Enumeration) C#排序比较 C#相等性比较 c#列举和迭代器 C#记录对象的变化 C#按需序列化对象为Json字符串 c#如何区分静态只读变量和常量 C#如何更好地理解引用类型和值类型 C#代理那点事儿 Pro ASP.NET MVC –第五章 使用Razor Pro ASP.NET MVC –第六章 MVC的基本工具 Pro ASP.NET MVC –第四章 语言特性精华
Customer IEnuramble Extension
On the road.... · 2014-03-07 · via 博客园 - On the road....
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));
        }
}