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 to create a new Telerik Data Access Domain Model based on the Northwind database.

The following code is the LINQ example.

C#


    using ( EntitiesModel dbContext = new EntitiesModel() )
    {
       IQueryable<string> countries = from customer in dbContext.Customers
                                      select customer.Country;
       IQueryable<string> distinctCountries = countries.Distinct();
       foreach ( string country in distinctCountries )
       {
           Console.WriteLine( country );                    
       }
    }

VB.NET


    Using dbContext As New EntitiesModel()
     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

See Also