private static void GroupByKeysSample() { List <Gamer> gamersList = GamerHelper.GetSampleGamers(); var gamersGroup = gamersList .GroupBy(item => new { item.TeamName, item.Gender }) .OrderBy(item => item.Key.TeamName) .ThenBy(item => item.Key.Gender).Select(item => new { TeamName = item.Key.TeamName, Gender = item.Key.Gender, Gamers = item //item.OrderBy(g => g.Name) }); foreach (var gamers in gamersGroup) { Console.WriteLine($"TeamName: {gamers.TeamName}, Gender: {gamers.Gender}\r\n" + $"Count: {gamers.Gamers.Count()}, MaxScore: {gamers.Gamers.Max(item => item.Score)}, " + $"MinScore: {gamers.Gamers.Min(item => item.Score)}, " + $"AverageScore: {gamers.Gamers.Average(item => item.Score)}, " + $"SumScore: {gamers.Gamers.Sum(item => item.Score)}"); foreach (Gamer gamer in gamers.Gamers) { Console.WriteLine(gamer); } } }
private static void GroupBySample() { List <Gamer> gamersList = GamerHelper.GetSampleGamers(); IEnumerable <IGrouping <string, Gamer> > gamerGroupEnumerable = gamersList.GroupBy(item => item.TeamName); foreach (IGrouping <string, Gamer> gamers in gamerGroupEnumerable) { Console.WriteLine($"" + $"teamNAme: {gamers.Key}\r\n count: {gamers.Count()}, maxScore: {gamers.Max(item => item.Score)}, " + $"minScore: {gamers.Min(item => item.Score)}, sumScore: {gamers.Sum(item => item.Score)}, " + $"AverageScore: {gamers.Average(item => item.Score)}"); foreach (Gamer gamer in gamers) { Console.WriteLine(gamer); } } }