我有一个像下面这样的列表
var stringCollection = {"X","1", "2","X","5", "10","X","7", "9"}
现在我需要一个List of List,使每个子列表看起来像下面的
SibList1 = {"X","1", "2"}
SibList2 = {"X","5", "10"}
SibList3 = {"X","7", "9"}
是否可以使用linq.
的问候Krish
试试这个
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();