简体   繁体   English

筛选和排序数组

[英]Filter and sort Array

I am working on a .net web application for School Management System. 我正在为学校管理系统开发.net Web应用程序。

There are 2 DTO objects Subject and Student 有2个DTO对象Subject和Student

Subject.cs Subject.cs

public class Subject
    {
        public string SubjectCode { get; set; }
        public string SubjectName { get; set; }
        public int StudentsEnrolled { get; set; }
        public IReadOnlyCollection<Student> Students { get; set; }
    }

Student.cs 学生.cs

public class Student
    {
        public string StudentId { get; set; }
        public string StudentName { get; set; }
        public int Marks { get; set; }
        public int Status { get; set; }
    }

There is an API method 'GetSubjectDetails' where we need to pass the subject name as argument. 有一个API方法“ GetSubjectDetails”,我们需要将主题名称作为参数传递。 The output is a Subject object with Subject details and list of students enrolled for that subject. 输出是一个“主题”对象,其中包含“主题”详细信息和该主题的注册学生列表。

Subject subject = GetSubjectDetails("Mathematics);

Now I have the Subject object with all the 50 students list in it. 现在,我有了“主题”对象,其中包含所有50个学生列表。 Now I need to filter the object with only 10 students who got the least marks in that subject. 现在,我只需要筛选10个在该学科中得分最低的学生即可。

How do I filter object. 如何过滤对象。 I can do it in either C#.net in server side or in the front end using typescript. 我可以使用打字稿在服务器端或前端的C#.net中进行操作。

您可以在服务器端bcs上使用LINQ加载所有对象,而在ui端进行过滤没有任何意义,因此您可以执行以下操作

subject.Students=subject.Students.OrderBy(s=>s.Marks).Take(10).ToList().AsReadOnly();

尝试这个 :

var students = subject.Students.OrderByDescending(x => x.Marks).Take(10).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM