Istudent Rana

Istudent Rana

  • 1.3k
  • 311
  • 79.1k

How to create a object from collection after grouping

Jan 11 2019 12:28 AM
I have list of Students which looks like below
 
List<StudentData> results= new List<StudentData>{
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<Course>{
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 )
{
}
}

Answers (5)