Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a comma delimited string from distinct list of values using LINQ?

Tags:

c#

linq

I have a list of values:

myCars

with values:

Ford
Ford
VW
Renault
Jeep
Jeep

which is referenced via:

myCars.Makers

I wish to create a distinct comma delimited string from this such that I get:

Ford,VW,Renault,Jeep

I am guessing that I need to run a distinct clause on myCars, but then am unsure about how to convert to comma delimited string as above.

Many thanks in advance

like image 646
SamJolly Avatar asked Feb 27 '14 15:02

SamJolly


1 Answers

use string.Join and Distinct

string.Join(",", myCars.Makers.Distinct());
like image 141
Selman Genç Avatar answered Nov 03 '22 21:11

Selman Genç