Your
ViewResult
class represents the data for
a single student. Your
foreach
loop would overwrite the properties of that class with the values for the last student returned.
That is, it
would do that,
if your code compiled. But your code won't compile. You attempt to store a
List<StudentData>
object in a property which holds a single
StudentData
object. And you then try to call that property as a method, which also won't work.
Try this:
var students = dt.StudentData
.Select(s => new
{
s.Id,
s.StudentClass,
s.StudentName,
Total = s.StudentMarks.Sum(m => m.Total),
Average = s.StudentMarks.Average(m => m.Total),
})
.OrderByDescending(s => s.Average)
.ToList();
This will give you a sorted list of anonymous types containing the student's data, their total score, and their average score.