linq:OrderByDescending 按指定表达式对集合倒序排序;延迟
第一步:查询出学生成绩表中的数据
using (var db = new SMSPModel.SMSPEntities()) { db.CommandTimeout = 0; var tb = (from p in db.tbExamMark where p.tbExamCourse.ID == this.DrplstExamCourse1.ExamCourseId && p.RecordStatus == decimal.One && p.tbExamCourse.RecordStatus == decimal.One && p.tbExamCourse.tbExamBasis.ID == this.DrplstExam1.ExamId && p.tbExamCourse.tbCourseBasis.RecordStatus == decimal.One && p.tbExamCourse.tbCourseBasis.tbSubjectBasis.RecordStatus == decimal.One && p.tbExamCourse.tbCourseBasis.tbSubjectBasis.tbAreaBasis.RecordStatus == decimal.One && p.tbStudentBasis.RecordStatus == decimal.One && p.tbStudentBasis.StudentStatus == decimal.One orderby p.tbStudentBasis.StudentCode select new ExamMark { StudentId = p.tbStudentBasis.ID, StudentCode = p.tbStudentBasis.StudentCode, StudentName = p.tbStudentBasis.StudentName, ClassName = (from p0 in db.tbClassStudent where p0.RecordStatus == decimal.One && p0.tbStudentBasis.ID == p.tbStudentBasis.ID && p0.tbClassBasis.tbYear.FromDate <= p.tbExamCourse.tbExamBasis.tbYear.FromDate && p0.tbClassBasis.tbYear.ToDate >= p.tbExamCourse.tbExamBasis.tbYear.ToDate && p0.tbClassBasis.tbYear.tbYearGroup.GroupNo == decimal.One select p0.tbClassBasis.ClassName).FirstOrDefault(), AppraiseMark = p.AppraiseMark == decimal.MinusOne ? null : (decimal?)p.AppraiseMark, Mark1 = p.Mark1 == decimal.MinusOne ? null : (decimal?)p.Mark1, Mark2 = p.Mark2 == decimal.MinusOne ? null : (decimal?)p.Mark2, TotalMark = p.TotalMark == decimal.MinusOne ? null : (decimal?)p.TotalMark, TotalGradeRank = p.TotalGradeRank == decimal.MinusOne ? null : (decimal?)p.TotalGradeRank, SegmentMark = p.SegmentMark == decimal.MinusOne ? null : (decimal?)p.SegmentMark, SegmentGradeRank = p.SegmentGradeRank == decimal.MinusOne ? null : (decimal?)p.SegmentGradeRank, LevelName = p.tbExamLevel.LevelName, GetPoint = p.tbExamLevel.LevelValue > decimal.Zero ? p.tbExamCourse.tbCourseBasis.CoursePoint : decimal.Zero, StudyPoint = p.tbExamCourse.tbCourseBasis.CoursePoint * (p.tbExamLevel.LevelName == "---" ? decimal.Zero : p.tbExamLevel.LevelValue) }).Distinct().OrderByDescending(c => c.SegmentMark).ToList();
第二步,根据SegmentMark 综合分 进行排名
//重新排名(综合分排名) var studentScoresRankList = (from p in tb select new ExamMark { StudentId = p.StudentId, StudentCode = p.StudentCode, StudentName = p.StudentName, ClassName = p.ClassName, AppraiseMark = p.AppraiseMark, Mark1 = p.Mark1, Mark2 = p.Mark2, TotalMark = p.TotalMark, TotalGradeRank = p.TotalGradeRank, SegmentMark = p.SegmentMark, LevelName = p.LevelName, GetPoint = p.GetPoint, StudyPoint = p.StudyPoint, SegmentGradeRank = tb.Select(c => c.SegmentMark).ToList().IndexOf(p.SegmentMark) + 1, }).OrderBy(c => c.TotalGradeRank).ToList(); return studentScoresRankList;
然后排名就出来了,然后重新绑定就可以了。