Click here to Skip to main content
15,945,877 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I am working on a simple student result. I want to achieve student Positions per class.

// I have this two models
 public class StudentData
        public int Id { get; set; }
 [Display(Name = "StudentClass")]
        public string StudentClass { get; set; } = null!;
[Display(Name = "StudentName")]
        public string StudentName { get; set; } = null!;
 public ICollection<StudentMark>? StudentMarks { get; set; }

 public class StudentMark

        public int MarkId { get; set; }
 [Display(Name = "Total")]
         public decimal? Total { get; set; }
  public StudentData? StudentData { get; set; }
// For View Purpose I added this model
 public class ViewResult

        public int Id { get; set; }
 [Display(Name = "Average")]
         public decimal? Average { get; set; }
[Display(Name = "Total Score")]
         public decimal? TotalScore { get; set; }
  public List <StudentData>? StudentData { get; set; }

What I have tried:

//On My controller I have tried this
ViewResult vs = new ViewResult();
 vs.StudentData = (from c in dt.StudentData
                                     select c).ToList();
                foreach(var item in vs.StudentData())
                    item.StudentMark = (from c in dt.StudentMark where c.RemarkId == item.Id select c).ToList();
                    vs.Total = (from c in dt.StudentMark where c.RemarkId == item.Id select c).Sum(c => c.Total);
                    vs.Average = (from c in dt.StudentMark where c.RemarkId == item.Id select c.Total).Average();

/* I want to order the search based on vs.Average so that the highest average scores will be the first position and if we have two or more students with same averages the positions will still be same. How can this be implemented..*/

Thank you very much for your precious time....
Updated 9-Aug-22 22:26pm
Richard MacCutchan 10-Aug-22 4:28am    
If you update your question then please add a detailed explanation of what has changed since the previous version.
Richard Deeming 11-Aug-22 4:22am    
Using the "compare" option under the v2 link, literally nothing changed.
Richard MacCutchan 11-Aug-22 4:48am    
Par for the course I guess.

1 solution

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
        Total = s.StudentMarks.Sum(m => m.Total),
        Average = s.StudentMarks.Average(m => m.Total),
    .OrderByDescending(s => s.Average)
This will give you a sorted list of anonymous types containing the student's data, their total score, and their average score.
Share this answer
Difameg Network Nigeria 10-Aug-22 4:31am    
Thanks for the response and your time, The ViewResult gets the list of all records instead of a single data.. Initially my view result was but public List <studentdata>? StudentData { get; set; }

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