Fastest way to Remove Duplicate Value from a list<> by lambda

前端 未结 7 1948
庸人自扰
庸人自扰 2020-12-01 00:55

what is fastest way to remove duplicate values from a list. Assume List longs = new List { 1, 2, 3, 4, 3, 2, 5 }; So I am interesting in

7条回答
  •  庸人自扰
    2020-12-01 01:03

    You can use this extension method for enumerables containing more complex types:

    IEnumerable distinctList = sourceList.DistinctBy(x => x.FooName);
    
    public static IEnumerable DistinctBy(
        this IEnumerable source,
        Func keySelector)
    {
        var knownKeys = new HashSet();
        return source.Where(element => knownKeys.Add(keySelector(element)));
    }
    

提交回复
热议问题