0

I have an array of comma delimited properties:

  • Red,Green
  • Green
  • Blue,Black
  • Yellow
  • Red,Black

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

  • Red
  • Green
  • Blue
  • Black
  • Yellow

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.

2
  • 5
    Split on the commas, merge all the values into one list and do a Distinct() on it, perhaps? Commented Nov 13, 2014 at 9:31
  • I haven't tried very many things since I just didn't know how to do it. Commented Nov 13, 2014 at 11:58

1 Answer 1

4

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();
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, that works with a simple array. Thank you. Unfortunately that does not work with Entity Framework and SQL Server. I get an error saying LINQ to Entities does not recognize the method 'System.String[] Split(Char[])'
Just call ToArray() before the SelectMany

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.