Distinct() with lambda?

前端 未结 18 1162
南旧
南旧 2020-11-22 06:04

Right, so I have an enumerable and wish to get distinct values from it.

Using System.Linq, there\'s of course an extension method called Distinct<

18条回答
  •  长发绾君心
    2020-11-22 06:34

    Here's a simple extension method that does what I need...

    public static class EnumerableExtensions
    {
        public static IEnumerable Distinct(this IEnumerable source, Func selector)
        {
            return source.GroupBy(selector).Select(x => x.Key);
        }
    }
    

    It's a shame they didn't bake a distinct method like this into the framework, but hey ho.

提交回复
热议问题