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 PlPosted Jan 11, 2019, 1:05 AM
YashiPosted Jan 11, 2019, 4:06 AM
Istudent RanaPosted Jan 11, 2019, 1:33 AM
.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()
});
Uma PlPosted Jan 11, 2019, 1:30 AM
Istudent RanaPosted Jan 11, 2019, 1:11 AM