1

Supposed I have a string array like this

{
"A“,
”B",
"A,B,D",
"C"
}

Is it possible that I write a single LinQ to get the distinct values {"A","B","C","D"} into a List?

3 Answers 3

6
lists.SelectMany(l => l.Split(',')).Distinct().ToList();
Sign up to request clarification or add additional context in comments.

Comments

3
var distinctValues = myList.SelectMany(x => x.Split(',')).Distinct().ToList();

This will split each string, and then flatten them into a single list, and get the distinct elements.

If you want to get the elements in alpha order, then you can tack on a .OrderBy(x => x) right before .ToList().

Comments

1

Fyi in linq query syntax its the same as the answers above

List s = new List() { "A","B","A,B,D","C"};

var result = (from x in s from y in x.Split(',') select y).Distinct().ToList();

Comments

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.