.Distinct() is a method that works with IEnumerable<T> and returns IEnumerable<T> (lazily evaluated). IEnumerable<T> - sequence: not a List<T> . Therefore, if you want to end the list, put .ToList() at the end.
// note: this first example does not compile List<string> str1 = (from item in stringList select item) // result: IEnumerable<string> .ToList() // result: List<string> .Distinct(); // result: IEnumerable<string> List<string> str2 = (from item in stringList select item) // result: IEnumerable<string> .Distinct() // result: IEnumerable<string> .ToList(); // result: List<string>
To illustrate why this is so, consider the following crude implementation of Distinct() :
public static IEnumerable<T> Distinct<T>(this IEnumerable<T> source) { var seen = new HashSet<T>(); foreach(var value in source) { if(seen.Add(value)) {
Marc gravell
source share