In LINQ, the Distinct method or operator is useful to get only distinct elements from the collection.
Following is the pictorial representation of the LINQ distinct method.
The LINQ distinct method will remove duplicate elements from the collection and return distinct or unique elements.
Following is the syntax of the distinct method to get unique elements from the collection.
C# Code
VB.NET Code
If you observe the above syntax, we applied the Distinct method on the “numbers” collection to get only distinct elements from the collection.
Following is the example of using the LINQ Distinct method.
C# Code
VB.NET
If you observe the above example, we applied the Distinct method with StringComparer.OrdinalIgnoreCase case property; otherwise, it will perform the operation with case sensitivity on “countries” collection, and it will treat “INDIA” and “india” as different.
Following is the result of the LINQ distinct method example.
This is how we can use LINQ distinct method in c#, vb.net to get unique elements from the collection.