Tech
Tech

Reputation: 661

How to convert array of comma delimited values to array with unique values?

I have an array of comma delimited properties:

Now I'd like to extract every unique colour from the array resulting in the following list:

Since I'm extracting the information from a database with Entity Framework in quite a complex structure I'd like to do the conversion in one statement if possible.

Upvotes: 0

Views: 151

Answers (1)

Alberto
Alberto

Reputation: 15951

Assuming your array of properties is like:

string[] properties = {"Red,Green","Green","Blue,Black","Yellow","Red,Black"};

you should split on comma and select the distinct values in this way:

string[] unique = properties.SelectMany(x=>x.Split(',')).Distinct().ToArray();

Upvotes: 4

Related Questions