Linq Distinct List

Linq Distinct List

Linq Distinct list. You want to see how to execute distinct query using Linq expression. Here are example in C#.net which shows stores distinct records in list. Let’s see how to remove duplicate items from list .

  1. Linq Chapter 1 – Split string to list of strings.
  2. Linq Chapter 2 – Remove duplicates from list.
  3. Linq Chapter 3 – Linq Where Clause.
  4. Linq Chapter 4 – Linq Group by multiple clause.
  5. Linq Chapter 5 – Linq Inner Join query.
  6. Linq Chapter 6 – Linq Distinct list.
  7. Linq Chapter 7 – Linq OfType Method.
C# Linq Distinct List Example.
void Main()
{

// var listOfDistinctOrders is equivalent to IEnumerable<Orders>

    var listOfDistinctOrders =  ( from orderList in Orders
                                  select orderList.ShipCity
                                 ).Distinct() ;                                   
    
}
VB.net Linq Distinct Query.
Private Sub Main()

	' var listOfDistinctOrders is equivalent to IEnumerable

	Dim listOfDistinctOrders = (From orderList In OrdersorderList.ShipCity).Distinct()

End Sub

SQL statement generated by Linq distinct query looks like below one.

SELECT DISTINCT [t0].[ShipCity]
FROM [Orders] AS [t0]

Extension method to get Distinct records in list.

Orders
   .Select (orderList => orderList.ShipCity)
   .Distinct ()

Output of Linq distinct query.

C# Linq Distinct List.

Below example in C# returns list of distinct shipment city and country.

void Main()
{

// var listOfDistinctOrders is equivalent to IEnumerable<Orders>

    var listOfDistinctOrders =  ( from orderList in Orders
                                  select new { City = orderList.ShipCity 
                                               , Region = orderList.ShipRegion
                                               }
                                 ).Distinct() ;                             
                                 
        listOfDistinctOrders.Dump();
    
}

Output.

Linq Distinct List c#.net example.

Lambda Expression and Extension methods.

Orders
   .Select (
      orderList => 
         new  
         {
            City = orderList.ShipCity, 
            Region = orderList.ShipRegion
         }
   )
   .Distinct ()

SQL statement distinct query equivalent to above Linq.

SELECT DISTINCT [t0].[ShipCity] AS [City], [t0].[ShipRegion] AS [Region]
FROM [Orders] AS [t0]

VB.net Distinct Example.

Private Sub Main()

	' var listOfDistinctOrders is equivalent to IEnumerable

	Dim listOfDistinctOrders = (From orderList In OrdersNew With { _
		Key .City = orderList.ShipCity, _
		Key .Region = orderList.ShipRegion _
	}).Distinct()

	listOfDistinctOrders.Dump()

End Sub

Tags:

Linq Inner Joinlistbox to listboxC# ArrayList Search using Linq Extension OfType Methodc# arraylistLinq Group By Multiple ColumnsLinq Split String To ListHow to select multiple columns in Linq- C#.net VB.net codeLinq Union and intersect ExampleLinq get max value from listListbox select multipleLearn Linq Step By Step

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.