Click here to Skip to main content
15,887,430 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all, I have a collection of strings in a generic list thus
C#
H,1000,28-09-2017
L,30,Pete
L,50,Pete
H,200,29-09-2017
L,35,Pete
L,45,Pete
L,55,Pete
etc....


How can I ( using Linq if possible ) count the number of "L" lines between each "H" line as I iterate over the list ? Basically the "H" indicates the start of a group containing n detail lines, e.g. for the first group I would like to form a string like
H,1000,28-09-2017,80,Pete,2 - where 2 = the number of L lines and 80 the sum of the amounts


What I have tried:

I currenly do it in a while loop summing and counting as I go
Posted
Updated 28-Sep-17 8:20am

1 solution

I strongly suspect a regular loop will be your best option. But if you really want to force it:
C#
IEnumerable<string> headerLinesWithCounts = source
    .Select((value, index) => (value: value, index: index))
    .Where(pair => pair.value[0] == 'H')
    .Select(pair => (value: pair.value, lines: source.Skip(pair.index + 1).TakeWhile(v => v[0] != 'H')))
    .Select(pair => $"{pair.value},{pair.lines.Count()}");
Output:
H,1000,28-09-2017,2 
H,200,29-09-2017,3 

Or, if you need to include the lines in the output as well:
C#
IEnumerable<string> headerLinesWithCounts = source
    .Select((value, index) => (value: value, index: index))
    .Where(pair => pair.value[0] == 'H')
    .Select(pair => (value: pair.value, lines: source.Skip(pair.index + 1).TakeWhile(v => v[0] != 'H')))
    .Select(pair => (header: $"{pair.value},{pair.lines.Count()}", lines: pair.lines))
    .SelectMany(pair => Enumerable.Repeat(pair.header, 1).Concat(pair.lines));
Output:
H,1000,28-09-2017,2 
L,30,Pete 
L,50,Pete 
H,200,29-09-2017,3 
L,35,Pete 
L,45,Pete 
L,55,Pete 


NB: This is using the new ValueTuple[^] type. If your compiler doesn't support it, you can use anonymous types instead.
 
Share this answer
 
v2
Comments
pkfox 28-Sep-17 14:29pm    
Thanks Richard I'll give that a shot
pkfox 29-Sep-17 1:03am    
Hi Richard this works a treat - how can I add the sum of the L lines ?
Richard Deeming 29-Sep-17 7:45am    
What do you mean by the sum of the lines?
pkfox 29-Sep-17 12:21pm    
The values in the L lines 30,35 etc...
Richard Deeming 29-Sep-17 12:53pm    
Create a function to extract the value from the line:
static int ExtractLineValue(string line)
{
    int index = line.IndexOf(',', 2);
    if (index == -1) index = line.Length;
    string part = line.Substring(2, index - 2);
    int.TryParse(part, out int result);
    return result;
}

Then add the sum to the header lines:
IEnumerable<string> linesWithCountsAndSums = source
    .Select((value, index) => (value: value, index: index))
    .Where(pair => pair.value[0] == 'H')
    .Select(pair => (value: pair.value, lines: source.Skip(pair.index + 1).TakeWhile(v => v[0] != 'H')))
    .Select(pair => (header: $"{pair.value},{pair.lines.Count()},{pair.lines.Sum(ExtractLineValue)}", lines: pair.lines))
    .SelectMany(pair => Enumerable.Repeat(pair.header, 1).Concat(pair.lines));

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900