2012-01-11 12 views

Odpowiedz

20

Można użyć metody .Distinct() przedłużacza.

var collectionWithDistinctElements = oldArray.Distinct().ToArray(); 
5

użyć metody Distinct w LINQ.

Zobacz http://msdn.microsoft.com/en-us/library/bb348436.aspx

  List<int> ages = new List<int> { 21, 46, 46, 55, 17, 21, 55, 55 }; 

      IEnumerable<int> distinctAges = ages.Distinct(); 

      Console.WriteLine("Distinct ages:"); 

      foreach (int age in distinctAges) 
      { 
       Console.WriteLine(age); 
      } 

      /* 
      This code produces the following output: 

      Distinct ages: 
      21 
      46 
      55 
      17 
      */ 
2

Distinct powinno wystarczyć problemu, ale jeśli robi to na zlecenie obiektu trzeba będzie wdrożyć IEquatable<T> i trzeba będzie zastąpić GetHashCode() sposób, aby to działało.

Powiązane problemy