I have StudentDB, which is just a list of Student objects, which is defined as:
public class Student
{
public String StudentID { get; set; }
public int ExtraCredit { get; set; }
public int QtyDemerits { get; set; }
public List<Demerits> LstDemerits = new List<Demerits>();
}
public class Demerits
{
public String TeacherID { get; set; }
public DateTime AsOf { get; set; }
public String Description1 { get; set; }
public String Description2 { get; set; }
}
I would like to compile a list of students, ordered by descending order of ExtraCredit, and see all of the student's Demerit details if they have any (i.e. TeacherID, AsOf, Description 1 & 2).
Here's my query, which is not quite yielding everything I want:
List<Student> StudentDB2 = new List<Student>();
StudentDB2 = StudentDB.OrderByDescending(x => x.ExtraCredit).Select(r => new Student
{
StudentID = r.StudentID,
ExtraCredit = r.ExtraCredit,
QtyDemerits = r.QtyDemerits,
LstDemerits = r.Demerits.OrderBy(c => c.TeacherID).ThenBy(d => d.AsOf).ToList()
})
.ToList();
dataGridView1.DataSource = StudentDB2;
When I run the code I'm only seeing 3 columns of data: StudentID, ExtraCredit, and QtyDemerits. But if QtyDemerits > 0, I'm not seeing the data in LstDemerits which should show Teacher Id and As of Date. What is wrong with my query?