CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
var solvedExercises = new List<DatabaseEntry>
{
new DatabaseEntry { StudentId = 1, ExerciseId = 1 },
new DatabaseEntry { StudentId = 1, ExerciseId = 2 },
new DatabaseEntry { StudentId = 2, ExerciseId = 2 },
new DatabaseEntry { StudentId = 3, ExerciseId = 1 },
new DatabaseEntry { StudentId = 3, ExerciseId = 2 },
new DatabaseEntry { StudentId = 3, ExerciseId = 3 },
};
var result1 = solvedExercises
.GroupBy(e => e.StudentId)
.ToDictionary(e => e.Key, e => e.Select(e2 => e2.ExerciseId).ToList());
foreach(var student in result1)
{
Console.Write($"Student #{ student.Key }: ");
foreach(var exercise in student.Value)
Console.Write($"{ exercise } ");
Console.WriteLine();
}
Console.WriteLine();
var result2 = solvedExercises
.GroupBy(e => e.StudentId)
.Select(e => new { e.Key, Count = e.Count() })
.ToDictionary(e => e.Key, e => e.Count);
foreach(var student in result2)
{
Console.WriteLine($"Student #{ student.Key }: { student.Value }");
}
}
}
public class DatabaseEntry
{
public int StudentId { get; set; }
public int ExerciseId { get; set; }
}