I have a string array in this format (each line is a slot in the array):
IT1
PID
Ref
Ref
REF
IT1
PID
REF
IT1
PID
Ref
REF
…
I want to extract each IT1 through the last REF into a new array using LINQ. The new result would be an IEnumerable<IEnumerable<string>>.
Using the above string slot array example, the new IEnumerable collection should have 3 IEnumerable<string> in it.
For example:
class 1
IT1
PID
Ref
Ref
REF
end class 1
class 2
IT1
PID
REF
end class 2
class 3
IT1
PID
Ref
REF
end class 3
…
Notice some sections of the array have 1 REF, some 2 REF and some 3 REF.
How can I use LINQ to extract each section from IT1 through the last REF into a new collection of IEnumerable<IEnumerable<string>>?
Psudo code…
var result = arrayData.Select(s => s.StartsWith("IT1")
.GroupBy(...)
.Select(result => new {IT1 through last ref goes here})
.ToArray();
Thanks all for the help!
You can partition the array using
GroupByand some temporary state like this:This will give you an array of arrays; each sub-array will start with an
"IT1"element and end with whatever your input had before the next"IT1". If your data is valid, that will be a"REF". If the data is invalid, you need to specify what should happen in this case.I ‘m not so sure how you expect these arrays to be converted into anonymous objects though. Anonymous objects might have an unknown type name, but they are still very strongly typed: you cannot dynamically decide the amount, names, and types of their members.