How to: Remove Duplicate Elements
This topic provides an example of how to remove duplicate elements from query results by using Distinct. The example uses the Distinct method to return the unique country names.
To run the code in this example, you need a fluent model based on the Northwind database.
The following code is the LINQ example.
using ( FluentModel dbContext = new FluentModel() )
{
IQueryable<string> countries = from customer in dbContext.Customers
select customer.Country;
IQueryable<string> distinctCountries = countries.Distinct();
foreach ( string country in distinctCountries )
{
Console.WriteLine( country );
}
}
Using dbContext As New FluentModel()
Dim countries As IQueryable(Of String) = From customer In dbContext.Customers
Select customer.Country
Dim distinctCountries As IQueryable(Of String) = countries.Distinct()
For Each country As String In distinctCountries
Console.WriteLine(country)
Next country
End Using