inquisitive_one
inquisitive_one

Reputation: 1495

Split a LINQ string list

I believe this could be another easy one for you LINQ masters out there.

I have a table with a field that consists of strings delimited by "#". After I select the field using LINQ, how can I split each of the strings into a different list? My string list looks like:

#A#B#C#D#G#F

I used a simple LINQ query to access this:

from x in Special_texts
where x.Name.Equals("ExceptionList")
select x.Content

In the end, my list should contain:

A
B
C
D
G
F

Thanks in advance.

Upvotes: 1

Views: 9567

Answers (2)

BrokenGlass
BrokenGlass

Reputation: 160862

Assuming you want a single list as output:

var list = Special_texts.Where(x=> x.Name.Equals("ExceptionList"))
                        .Select(x=> x.Content)
                        .AsEnumerable()
                        .Select(x=> x.Split(new [] {'#'}, StringSplitOptions.RemoveEmptyEntries))
                        .SelectMany(x=> x)
                        .ToList();

Alternatively if you want a list of lists (one for each item in Special_texts):

var list = Special_texts.Where(x=> x.Name.Equals("ExceptionList"))
                        .Select(x=> x.Content)
                        .AsEnumerable() 
                        .Select(x=> x.Split(new [] {'#'}, StringSplitOptions.RemoveEmptyEntries).ToList())
                        .ToList();

Upvotes: 7

Akhil
Akhil

Reputation: 7600

from x in Special_texts
where x.Name.Equals("ExceptionList")
select x.Content.Split('#').ToList();

Upvotes: 2

Related Questions