Your specification is not complete. What do you want with this input:
Level 01
Level 02
-- Blank Line --
Level 03
Level 04
-- Blank Line --
And how about two contiguous blank lines?
Level 01
Level 01
-- Blank Line --
-- Blank Line --
Level 02
And what do you want if your input sequence is empty? An empty output, or only a blank line?
Let's assume you don't have this. You requirement is:
Given an input sequence of strings, without two contiguous blank lines. Take the first string, skip all strings until the first empty string. Take the empty string. Repeat this until the end of your sequence.
So you take the first "Level 01", skip all lines until the blank line, you take the blank line, and the first line after that: "Level 02". Skip all lines until the next blank line, take this next blank line, etc.
It might be that "empty string" in fact is "-- Blank Line --". I assume you are smart enough to change the code accordingly.
You can do that in one big LINQ statement. I think your code will be much easier to read, understand, reuse, test, maintain if you make an extension method. If you are not familiar with extension methods, see extension methods demystified
static IEnumerable<string> SkipContiguousLines(this IEnumerable<string> source)
{
// TODO implement
}
Let's first see if the interface is like you want:
IEnumerable<string> lines = ... // get the input
IEnumerable<string> remainingLines = lines.SkipContiguous();
Or in a bigger LINQ:
var result = orderLines.GroupBy(orderLine => orderLine.OrderId)
.Select(group => new
{
OrderId = group.Key,
OrderLines = group.SkipContiguous(),
});
The code is fairly straightforward:
static IEnumerable<string> SkipContiguousLines(this IEnumerable<string> lines)
{
string blankLine = String.Empty; // or ----- Blank Line ---- ?
using (var lineIterator in lines.GetEnumerator())
{
bool lineAvailable = lineIterator.MoveNext();
while (lineAvailable)
{
// there is still something in your input:
yield return lineIterator.Current;
// skip until blank line:
while (lineAvailable && lineIterator.Current != blankLine)
{
lineAvailable = lineIterator.MoveNext();
}
// if not at end, you found a blank line: return the blank line
if (lineAvailable) yield return blankLine;
}
}
}