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

推荐订阅源

T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
F
Fortinet All Blogs
B
Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Security Blog
Microsoft Security Blog
Blog — PlanetScale
Blog — PlanetScale
月光博客
月光博客
腾讯CDC
小众软件
小众软件
G
Google Developers Blog
V
Visual Studio Blog
罗磊的独立博客
GbyAI
GbyAI
V
V2EX
大猫的无限游戏
大猫的无限游戏
H
Help Net Security
L
LangChain Blog
Engineering at Meta
Engineering at Meta
量子位
The GitHub Blog
The GitHub Blog
博客园 - 司徒正美
WordPress大学
WordPress大学
B
Blog RSS Feed

博客园 - 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));
        }
}