0

Below I have code that takes a test file, splits it into two groups: Apollo and Sabre, and is supposed to tell me how many times the word "Processed" is used in each group, however whenever I run this, it just tells me how many lines the file is, which I already know. Could someone please explain why this is not working and a solution on how to fix this?

            var lines1 = File.ReadLines(path);
            List<string> apollo = lines1.Take(7678).ToList();
            List<string> sabre = lines.Skip(7678).Take(5292).ToList();
            var g = apollo.GroupBy(i => i);
            foreach (var grp in g)
            {
                Console.WriteLine("{0} {1}", grp.Key, grp.Count());
            }
venerik
  • 5,766
  • 2
  • 33
  • 43
  • possible duplicate of [c#: a method to count occurrences in a list](http://stackoverflow.com/questions/1139181/c-a-method-to-count-occurrences-in-a-list) – Michael May 28 '15 at 21:54
  • 3
    well, you are looking for word "Processed" right? but how can program know about it, if this word does not met in code? – Arsen Mkrtchyan May 28 '15 at 21:56
  • Where is the 7678 coming from? Is there any chance you know at which character the appollo/sabre split happens? That would make a solution fairly easy. – Quality Catalyst May 28 '15 at 22:09
  • @QualityCatalyst 7678 is the line at which apollo ends. I ma not sure what you mean by where what character split, but the last character on line 7678 is an asterisk, or *. Hope that helps? – Patrick Overmyer May 29 '15 at 13:23
  • @ArsenMkrtchyan Processed is in the text file so since I put the lines in a list it should be in the code – Patrick Overmyer May 29 '15 at 13:24

1 Answers1

1

Perhaps you need to actually check for the value:

var g = apollo
    .Where(line => line == "Processed")
    .GroupBy(i => i);

However - perhaps you can just use Count()

var apoloCount = apollo.Count(line => line == "Processed");
var sabreCount = apollo.Count(line => line == "Processed");

If the lines contain multiple words (unclear from your question), you can do something like this:

var apoloCount = apollo
    .SelectMany(line => line.Split(' ')) //Get the individual words from the line
    .Count(word => word == "Processed");
Dave Bish
  • 19,263
  • 7
  • 46
  • 63
  • As far as I understand there can be multiple characters/words on a single line hence your suggested solution won't work. – Quality Catalyst May 28 '15 at 21:57
  • `var sabreCount = apollo.Count(line => line == "Processed");` seems incorrect. – Quality Catalyst May 28 '15 at 22:05
  • @QualityCatalyst not sure what you think seems incorrect about it. Additionally, the second part of Dave's answer answers your first question quite well and while I'm only compiling in my head, seems the perfect solution. – crthompson May 28 '15 at 22:18
  • `apolloCount` and `sabreCount` have always the same value as they perform the same operation. Shouldn't `sabreCount` be based on `sabre`? – Quality Catalyst May 28 '15 at 22:23
  • The code seems to work fine, but I am finding a little difficulty getting the console to tell me the number. – Patrick Overmyer May 29 '15 at 13:32