using System.Collections.Generic;
public static void Main()
var source = new List<string>
IEnumerable<string> linesWithCountsAndSums = source
.Select((value, index) => new { value, index })
.Where(pair => pair.value[0] == 'H')
.Select(pair => new { pair.value, lines = source.Skip(pair.index + 1).TakeWhile(v => v[0] != 'H') })
.Select(pair => new { header = $"{pair.value},{pair.lines.Count()},{pair.lines.Sum(ExtractLineValue)}", lines = pair.lines })
.SelectMany(pair => Enumerable.Repeat(pair.header, 1).Concat(pair.lines));
foreach (string line in linesWithCountsAndSums)
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);