user662285 user662285 - 1 year ago 87
C# Question

show only distinct 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))


Answer Source

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

String.Join(",", g.Select(s => s.ProductName).Distinct())
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download