1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
using System;
using System.Collections.Generic;
using System.Linq;
namespace gfoidl.Linq
{
public static class LinqExtensions
{
public static List<T> Unique<K, T>(this List<T> inputList, Func<T, K> func)
{
#region Input validation
if (func == null)
throw new ArgumentNullException("Key selector function cannot be null");
if (inputList == null)
return null;
if (inputList.Count == 0)
return new List<T>();
#endregion
var grp = inputList.GroupBy(func);
return grp.Select(g => g.First()).ToList();
}
}
}
|