Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
1 like 0 dislike
1.2k views
in .Net Framework by 66 69 85

Using Linq in.NET Core, I'm attempting to retrieve only distinct values for a specific column from my table. How can I use Linq to get just unique values?

I tried some complex methods, like a for loop and an if statement, but I think there is a simpler approach.

How To Select Only Unique Value in Linq C#?


1 Answer

1 like 0 dislike
by 66 69 85
 
Best answer

How to Select Only Unique Value in Linq C#?

Simply you need to use the .Distinct() method in your code.

For example, you have a list of Products with duplicates of ProductName and you want to get only the unique ProductName :

var result = allProducts.Select(p => p.ProductName ).Distinct();

It will return only the ProductName and the unique Name.

If you don’t ask, the answer is always NO!
...