Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List<comma-separated strings> => List<string>?

Tags:

c#

linq

Trying to come up with a LINQy way to do this, but nothing's coming to me.

I have a List<> of objects which include a property which is a comma-separated list of alpha codes:

lst[0].codes = "AA,BB,DD"
lst[1].codes = "AA,DD,EE"
lst[2].codes = "GG,JJ"

I'd like a list of those codes, hopefully in the form of a List of strings:

result = AA,BB,DD,EE,GG,JJ

Thanks for any direction.

like image 906
john paz Avatar asked Jan 15 '16 15:01

john paz


People also ask

How do you add a comma separated string to a list in Java?

List<String> items = Arrays. asList(commaSeparated. split(",")); That should work for you.

How do I convert a list to a comma separated string in Python?

How to Convert a Python List into a Comma-Separated String? You can use the . join string method to convert a list into a string. So again, the syntax is [seperator].

How do I create a comma separated string from a list of strings in C#?

The standard solution to convert a List<string> to a comma-separated string in C# is using the string. Join() method. It concatenates members of the specified collection using the specified delimiter between each item.


1 Answers

Use SelectMany to get all split codes and use Distinct to not repeat the values. Try something like this:

var result = lst.SelectMany(x => x.codes.Split(",")).Distinct().ToList();
like image 56
Joel R Michaliszen Avatar answered Oct 02 '22 13:10

Joel R Michaliszen