user662285 user662285 - 2 months ago 6
C# Question

show only distinct value.in String.Join

I am using below code to append diffrent Product with comma but the problem is if i get the same name again then how to show only distinct value.

Eg-> AA,BB,CC,DD - This is correct
but in some scenarios i get value as AA,AA,AA,AA then i want to show as 'AA' only.


I am currently using as :

ProductName = String.Join(",", g.Select(s => s.ProductName))


Thanks.

Answer

You can use the Distinct() extension method on the result of your Select method call.

String.Join(",", g.Select(s => s.ProductName).Distinct())
Comments