Performing a subtotal on filtered data from a streamreader

后端 未结 2 1890
盖世英雄少女心
盖世英雄少女心 2021-01-26 06:01

edit as question is unanswered

I have a filtered output based on 1 criteria (first 3 numbers are 110,210 or 310,to give 3 distinct groups) to console from streamreader.

2条回答
  •  别那么骄傲
    2021-01-26 07:03

    string input = File.ReadAllText("file.dat");
    var result = Regex.Matches(input, "(210|310|410).*?([A-C]{3})([0-9]{5})")
        .Cast()
        .Select(m => new { 
            P1 = m.Groups[1].Value, 
            P2 = m.Groups[2].Value, 
            P3 = Convert.ToInt32(m.Groups[3].Value)
        })
        .GroupBy(x => new{x.P1,x.P2})
        .Select(x=>String.Format("{0} {1} {2}",x.Key.P1,x.Key.P2,x.Sum(y=>y.P3)))
        .ToList();
    

提交回复
热议问题