I have list of Students which looks like below
List results= new List{
new Student{Id = 1, Name = "John", CourseName = "Math", CourseStatus ="Complete"},
new Student{Id = 1, Name = "John", CourseName = "Science", CourseStatus ="In Complete"},
new Student{Id = 1, Name = "John", CourseName = "English", CourseStatus ="Complete"},
new Student{Id = 2, Name = "Sarah", CourseName = "Math", CourseStatus ="Complete"},
new Student{Id = 2, Name = "Sarah", CourseName = "Computer", CourseStatus ="Complete"},
new Student{Id = 2, Name = "John Doe", CourseName = "Account", CourseStatus ="Complete"},
}
I want to create a Student with list of Course out from the above collection of data.
Student john = new Student();
john.Id = 1 , john.Name = "John", john.Courses = new List{
new Course{Name = "Math", CourseStatus ="Complete"},
new Course{Name = "Science", CourseStatus ="In Complete"},
new Course{Name = "English", CourseStatus ="Complete"},
}
How do i achieve this ?
What I have done so far is
var query= results.GroupBy(s=> s.Id);
foreach (var group in query)
{
foreach (var student in group )
{
}
}

Uma PlRank #1,333 · 1.3K reputationPosted Jan 10, 2019, 8:05 PM EST
Istudent RanaRank #4,270 · 354 reputationPosted Jan 10, 2019, 8:11 PM EST
Uma PlRank #1,333 · 1.3K reputationPosted Jan 10, 2019, 8:30 PM EST
Istudent RanaRank #4,270 · 354 reputationPosted Jan 10, 2019, 8:33 PM EST
.Select(grp => new Student{
Id= grp.Key.Id,
Name= grp.Key.Name,
Courses = grp.Select(x=>new Course
{
Name = x.CourseName,
Status = x.CourseStatus
}).ToList()
});
Yashi97 reputationPosted Jan 10, 2019, 11:06 PM EST