Click here to Skip to main content
13,898,214 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
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 tried:

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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
   
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()
});
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Take a look at below example:
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!
   
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web05 | 2.8.190306.1 | Last Updated 11 Jan 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100