2009-07-16 14 views
9

Sto usando .NET 3.5. Perché sto ancora essere sempre:. Elenco .NET. Distinto

non contiene una definizione per 'distinta'

con questo codice:

using System.Collections.Generic; 

     //.. . . . . code 


    List<string> Words = new List<string>(); 
     // many strings added here . . . 
    Words = Words.Distinct().ToList(); 

risposta

35

Sei

using System.Linq; 

?

Distinct è un metodo di estensione definito in System.Linq.Enumerable quindi è necessario aggiungerlo utilizzando l'istruzione.

E non dimenticare di aggiungere un riferimento a System.Core.dll (se stai usando VS2008, questo è già stato fatto per te).

+0

+1 per System.Core –

+1

C'è una ragione per la denominazione che come quello? –

+1

Martinho - naming _what_ like _what_? –

5

Hai dimenticato di aggiungere

using System.Linq; 

Distinct è un extension method che è definito nel System.Linq.Enumerable, così si può solo chiamare se si importa quel namespace.

È inoltre necessario aggiungere un riferimento a System.Core.dll.
Se il progetto è stato creato come progetto .Net 3.5, sarà già referenziato; se lo hai aggiornato da .Net 2 o 3, dovrai aggiungere tu stesso il riferimento.

-1
List<string> words = new List<string>(); 

// many strings added here . . . 

IEnumerable <string> distinctword =Words .distinct(); 

foreach(string index in distinctword) 
{ 
     // do what u want here . . . 
} 
+2

Fornire una correzione non risponde alla domanda "Perché". –

0

da MSDN blog: Charlie Calvert MSDN Blog Link

da usare su .net fiddle: Tipo --project: Console

using System; 
using System.Collections.Generic; 
using System.Linq; 

public class Program 
{ 
    public static void Main() 
    { 
     Console.WriteLine("Hello World"); 
     var listA = new List<int> { 1, 2, 3, 3, 2, 1 }; 
     var listB = listA.Distinct(); 

     foreach (var item in listB) 
     { 
      Console.WriteLine(item); 
     } 
    } 
} 
// output: 1,2,3