3

I read this question: Split List into Sublists with LINQ but it not help me for the following problem.

I have the following list

18
0
abcde
678
-----
23
1
abcde

-----
66
4
3rwer
1
another item

How can I split this list into sublist by ----- separator ?

Thanks

Community
  • 1
  • 1
Snake Eyes
  • 16,287
  • 34
  • 113
  • 221

1 Answers1

3

I'm not aware of an existing operator to do this in LINQ to Objects itself, more the MoreLINQ project has the Split method, so you can use:

var sections = originalList.Split("-----");

That returns an IEnumerable<IEnumerable<string>> - if you need a list of lists, you could use:

var sections = originalList.Split("-----")
                           .Select(section => section.ToList())
                           .ToList();

MoreLINQ has a NuGet package that you might wish to use for installation.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    That [`Split`](https://code.google.com/p/morelinq/source/browse/MoreLinq/Split.cs) has a tiny mistake on line 179: `if (source == null) throw new ArgumentNullException("separatorFunc");` probably should be `separatorFunc == null`. – dbc Sep 23 '14 at 16:33
  • 1
    @dbc: Yes, you're right. I'll fix that. Still, it works for other cases :) – Jon Skeet Sep 23 '14 at 16:36