Performing a subtotal on filtered data from a streamreader

后端 未结 2 1887
盖世英雄少女心
盖世英雄少女心 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 06:45

    (Posting this answer here, as the other question is closed.) Using ReadAllText will be inefficient for large files.

    public static class LinqToTextReader {
        public static IEnumerable AsEnumerable(this TextReader reader) {
            string line;
            while ((line = reader.ReadLine()) != null) {
                yield return line;
            }
        }
    }
    
    class Program {
        static void Main(string[] args) {
            using (StreamReader reader = new StreamReader("file.dat")) {
                var locations = new Dictionary() {
                    {"210", new [] {406, 409, 129, 140, 142, 153}},
                    {"310", new [] {322, 325, 113, 124, 126, 137}},
                    {"410", new [] {478, 481, 113, 124, 126, 137}}
                };
    
                var query =
                    from line in reader.AsEnumerable()
                    let lineStart = line.Substring(0, 3)
                    where lineStart == "210" || lineStart == "310" || lineStart == "410"
                    let currentLocations = locations[lineStart]
                    select new {
                        letters = line.Substring(currentLocations[0], currentLocations[1]),
                        value =
                            int.Parse(line.Substring(currentLocations[2], currentLocations[3])) +
                            int.Parse(line.Substring(currentLocations[4], currentLocations[5]))
                    };
    
                //It should be possible to combine the two queries
                var query2 = 
                    from item in query
                    group item by item.letters into letterGroup
                    select new {
                        letters = letterGroup.Key,
                        total = letterGroup.Sum(item => item.value)
                    };
    
                foreach (var item in query2) {
                    Console.WriteLine(item.letters);
                    Console.WriteLine(item.total);
                }
            }
        }
    }
    

提交回复
热议问题