3

I would like to get the other number of games the same person has played in on. For example lisa plays in game 7 which is 1 other game. Tim and Josh both play in game 2 but also play in 3 other games. Is there a way through Igrouping to compare groups and see if the values are the same?

public List<Game> DummyDataSet()
{
    dataSet.Add(new Game { GameNo = 1, FirstName = "Lisa" });
    dataSet.Add(new Game { GameNo= 2, FirstName = "Tim" });
    dataSet.Add(new Game { GameNo = 2, FirstName = "Josh" });
    dataSet.Add(new Game { GameNo = 3, FirstName = "Susan" });
    dataSet.Add(new Gamee { GameNo = 4, FirstName = "Tim" });
    dataSet.Add(new Gamee { GameNo = 5, FirstName = "Tim" });
    dataSet.Add(new Gamee { GameNo = 5, FirstName = "Josh" });
    dataSet.Add(new Game { GameNo = 6, FirstName = "Josh" });
    dataSet.Add(new Game { GameNo = 7, FirstName = "Lisa" });

    return dataSet;
}

public void numOfOtherMissions()
{
    List<Game> something;
    something = DummyDataSet();
    var grouped = something.ToLookup(x => x.GameNo, x => x.FirstName);
    foreach (IGrouping<int, string> item in grouped)
    {
        Console.Write(item.Key);
        Console.Write(": ");
        var result = grouped.ToLookup(z => FirstName);
        foreach (var value in item)
        {
            int games = 0;
            if(result == item)
            {
                othergames++;
            }
            else
            {
                othergames = 0;
            }
            Console.Write(value + " " + othergames);
            Console.WriteLine();
        }
    }
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Allee Clark
  • 149
  • 1
  • 14
  • What exactly do you want to get? Do you want to group games by the exact players that are in them? Can you give an example output? – Yacoub Massad Jun 05 '16 at 18:53
  • I can't see Sam or Ilya in your example. Please provide the output for the example that you are providing in the question. – Yacoub Massad Jun 05 '16 at 19:00
  • Right now the output looks like this. The first grouping allowed me to get the same keys grouped with each value. Next each name should show how many other games theyve played. I'm guessing the best way is to compare keys that are not the same and see if values are the same. `GameNo 1: Lisa 0 GameNo 2: Tim 0 Josh 0 GameNo 3: Susan 0 GameNo 4: Tim 0 GameNo 5: Tim 0 Josh 0 GameNo 6: Josh 0 GameNo 7: Lisa 0 ` – Allee Clark Jun 05 '16 at 19:06
  • Output should look like `GameNo 1: Lisa 1 GameNo : Tim 2 Josh 2 GameNo 3: Susan 0 GameNo 4: Tim 2 GameNo 5: Tim 2 Josh 2 GameNo 6: Josh 2 GameNo 7: Lisa 1 ` – Allee Clark Jun 05 '16 at 19:08

1 Answers1

1

You cannot use the current lookup to get the information that you want. You need to query the list again to get the number of games for each person. Here is an exmaple:

public void numOfOtherMissions(List<Game> something)
{
    var grouped = something.ToLookup(x => x.GameNo, x => x.FirstName);

    //Create a dictionary that holds the number of games for each person
    var gamesForPerson =
        something
        .GroupBy(x => x.FirstName)
        .ToDictionary(x => x.Key, x => x.Count());

    foreach (IGrouping<int, string> item in grouped)
    {
        Console.Write(item.Key);
        Console.Write(": ");

        foreach (var value in item)
        {
            //Get total number of games for this person and subtract 1
            var othergames = gamesForPerson[value] - 1;

            Console.Write(value + " " + othergames);
            Console.WriteLine();
        }
    }
}
Yacoub Massad
  • 27,509
  • 2
  • 36
  • 62