Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have list of Students which looks like below

C#
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.

C#
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 tried:

C#
var query= results.GroupBy(s=> s.Id);


Student st = new Student();
foreach (var group in query)
{
<pre> st.myCourses = new List<MyCourse>();
                foreach (var st in group )
                {
                        MyCourse c = new MyCourse();
                        c.Name= st.CourseName;
                        c.Status = st.Status;

                     st.myCourses.Add(c);
                }


}
Posted
Updated 10-Jan-19 20:46pm
v2

Take a look at below example:
C#
void Main()
{

	List<Student> StudentData= new List<Student>{
		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 = 3, Name = "John Doe", CourseName = "Account", CourseStatus ="Complete"}, 
		};

	List<Studentv2> result = StudentData
		.GroupBy(x => x.Id)
		.Select(grp => new Studentv2()
			{
				StudentId = grp.Key,
				StudentName = grp.Select(x => x.Name).First(),
				Courses = grp.Select((x,y) => new Course
					{
						CourseId = y+1,
						CourseName = x.CourseName,
						CourseStatus = x.CourseStatus
					}).ToList()
			})
		.ToList();
	//a result list is ready to use ;)

}

// Define other methods and classes here
public class Student
{
	public int Id=0;
	public string Name = string.Empty;
	public string CourseName = string.Empty;
	public string CourseStatus = string.Empty;
}

public class Studentv2
{
	public int StudentId=0;
	public string StudentName = string.Empty;
	public List<Course> Courses = null; 
}

public class Course
{
	public int CourseId = 0;
	public string CourseName = string.Empty;
	public string CourseStatus = string.Empty;

}


Good luck!
 
Share this answer
 
v2
Have a look at this: Using Linq to create a Dictionary of sub-Lists by grouping from a collection.[^] - it doesn't do exactly what you want, but it shows the hard bit, which is generating the collection. Passing that to a Student constructor shouldn't be too difficult.
 
Share this answer
 
Comments
istudent 11-Jan-19 2:19am    
In my above what I have tried section, I could not able to assign id and name for one Student. List of Courses is good. But I just want only one Id and one Name from those collection.
istudent 11-Jan-19 2:34am    
this is what I did
this is what i did

var stData= results.GroupBy(s=> new { s.Id, s.Name})
.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()
});

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900