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#?
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.
Most popular tags