0

I have a list like the following

var stringCollection = {"X","1", "2","X","5", "10","X","7", "9"}

Now I need a List of List such that each sub list looks like the following

SibList1 = {"X","1", "2"}
SibList2 = {"X","5", "10"}
SibList3 = {"X","7", "9"}

Is it possible using linq.

Regards Krish

RADKrish
  • 51
  • 7
  • 1
    possible duplicate of [Split List into Sublists with LINQ](http://stackoverflow.com/questions/419019/split-list-into-sublists-with-linq) – Huy Hoang Pham May 19 '15 at 03:45
  • 1
    Is each sub list 3 elements, or can it vary, such as it just resets when you encounter an x again? – sovemp May 19 '15 at 03:50
  • 1
    @sovemp either way there is a good enough duplicate : [LINQ operator to split List of doubles to multiple list of double based on generic delimiter](http://stackoverflow.com/questions/13194898/linq-operator-to-split-list-of-doubles-to-multiple-list-of-double-based-on-gener) – har07 May 19 '15 at 04:41

1 Answers1

2

Try this

string[] stringCollection = { "X", "1", "2", "X", "5", "10", "X", "7", "9" };
int index = 0;
var subLists = stringCollection
    .Select(x => new { i = (x == "X" ? ++index : index), s = x })
    .GroupBy(x => x.i)
    .Select(x => x.Select(y => y.s).ToArray())
    .ToArray();
Nacho
  • 962
  • 6
  • 14