You need to use groupBy, and a simple manual mapping:
class Program
{
public class Course
{
public int Id { get; set; }
public string Name { get; set; }
public int Grade { get; set; }
}
static void Main(string[] args)
{
var courses = new List<Course>{
new Course(){Id=30120,Name="John Doe",Grade=12},
new Course(){Id=30120,Name="John Doe",Grade=13},
new Course(){Id=30120,Name="John Doe",Grade=14},
};
var result = courses
.GroupBy(a => new { a.Id, a.Name })
.Select(m => Map(m));
Console.WriteLine(JsonSerializer.Serialize(result));
}
private static object Map(IGrouping<dynamic, Course> arg1)
{
var course = new Dictionary<string, dynamic>();
course.Add("Id", arg1.Key.Id);
course.Add("Name", arg1.Key.Name);
int index = 1;
arg1.ToList().ForEach((p) =>
{
course.Add($"Grade{index++}", p.Grade);
});
return course;
}
}
The only thing you need is to create a table from the dictionary.