Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET List.Distinct

I'm using .NET 3.5. Why am I still be getting:

does not contain a definition for 'Distinct'

with this code:

using System.Collections.Generic;

       //.. . . . . code


    List<string> Words = new List<string>();
       // many strings added here . . .
    Words = Words.Distinct().ToList();
like image 837
Nona Urbiz Avatar asked Jul 16 '09 16:07

Nona Urbiz


People also ask

How do I get distinct data from a list?

Using Python's import numpy, the unique elements in the array are also obtained. In the first step convert the list to x=numpy. array(list) and then use numpy. unique(x) function to get the unique values from the list.

What is distinct () in C#?

C# Linq Distinct() method removes the duplicate elements from a sequence (list) and returns the distinct elements from a single data source. It comes under the Set operators' category in LINQ query operators, and the method works the same way as the DISTINCT directive in Structured Query Language (SQL).

How do I get distinct on a single column in LINQ?

distinct in Linq to get result based on one field of the table (so do not require a whole duplicated records from table). I know writing basic query using distinct as followed: var query = (from r in table1 orderby r. Text select r).

What is List distinct?

Returns a list that contains all the values in list list with duplicates removed.


1 Answers

Are you

using System.Linq;

?

Distinct is an extension method defined in System.Linq.Enumerable so you need to add that using statement.

And don't forget to add a reference to System.Core.dll (if you're using VS2008, this has already been done for you).

like image 139
R. Martinho Fernandes Avatar answered Sep 18 '22 20:09

R. Martinho Fernandes