Как работает group by? - C#
Формулировка задачи:
Как работает group by тут? Объясните код пожалуйста)
Объясните как это работает?
" var participants = GetAll().ToList();
var result = participants.GroupBy(p => p.Gender).Select(g => new { Gender = g.Key, Participants = g.OrderBy(p => p.Time).Take(3) });"
Листинг программы
- namespace consoleApp
- {
- public class Program
- {
- static void Main(string[] args)
- {
- var participants = GetAll().ToList();
- var result = participants.GroupBy(p => p.Gender).Select(g => new { Gender = g.Key, Participants = g.OrderBy(p => p.Time).Take(3) });
- foreach (var group in result)
- {
- Console.WriteLine(group.Gender);
- foreach (var participant in group.Participants)
- {
- Console.WriteLine(participant.ToString());
- Console.ReadLine();
- }
- }
- }
- public static Participant[] GetAll()
- {
- return new Participant[]{
- new Participant("Alex", "g1", Sex.Male, TimeSpan.FromSeconds(67)),
- new Participant("Mark", "g2", Sex.Male, TimeSpan.FromSeconds(98)),
- new Participant("Ann", "g3", Sex.Female, TimeSpan.FromSeconds(123)),
- new Participant("Kate", "g2", Sex.Female, TimeSpan.FromSeconds(75)),
- new Participant("Peter", "g3", Sex.Male, TimeSpan.FromSeconds(69)),
- };
- }
- }
- public struct Participant
- {
- public string Surname;
- public string Group;
- public Sex Gender;
- public TimeSpan Time;
- public Participant(string surname, string group, Sex gender, TimeSpan time)
- {
- Surname = surname;
- Group = group;
- Gender = gender;
- Time = time;
- }
- public override string ToString()
- {
- return $"{Surname} {Group} {Gender} {Time}";
- }
- }
- public enum Sex { Male = 1, Female = 2 };
- }
Решение задачи: «Как работает group by?»
textual
Листинг программы
- var participants = GetAll().ToList();
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д