Linq最佳实践推动了物业

时间:2017-05-28 14:21:22

标签: c# linq

学生班级的Property Grade / GradeLinq由积分和得分列表驱动。

我找到了两种不同的方法来计算成绩。如下所示为Grade和GradeLinq

良好的班级设计的最佳实践是什么?提供不利/优势或更好的解决方案。

得分等级

public class Scoring
{
    public int minScale { get; set; }
    public int maxScale { get; set; }
    public string Grade { get; set; }
}

public class Student
{

    public int StudentID { get; set; }
    public string StudentName { get; set; }
    public int StandardID { get; set; }
    public int Points { get; set; }
    public string Grade { get; set; }

    public static List<Scoring> sc { get; set; }
    public string GradeLinq
    {
        get
        {
            return sc?.Find(w => w.minScale < Points && w.maxScale > Points).Grade;
        }
    }
}

var scoringList = new List<Scoring>
{
    new Scoring { maxScale=100, minScale=85, Grade ="A" },
    new Scoring { maxScale=84, minScale=70, Grade ="B" },
    new Scoring { maxScale=69, minScale=50, Grade ="C" },
    new Scoring { maxScale=49, minScale=40, Grade ="D" },
    new Scoring { maxScale=39, minScale=25, Grade ="E" },
    new Scoring { maxScale=24, minScale=0, Grade ="F" },
};



var studentList = new List<Student>()
{
    new Student() { StudentID = 1, StudentName = "John", StandardID =1, Points = 88 },
    new Student() { StudentID = 2, StudentName = "Moin", StandardID =1, Points = 60 },
    new Student() { StudentID = 3, StudentName = "Bill", StandardID =2, Points = 61 },
    new Student() { StudentID = 4, StudentName = "Ram",  StandardID =2, Points = 99 },
    new Student() { StudentID = 5, StudentName = "Ron", Points = 10 }
};


void Main()
{
    Student.sc = scoringList;
    studentList.ForEach(f => f.Grade = scoringList.Find(w => w.minScale < f.Points && w.maxScale > f.Points).Grade);
}

1 个答案:

答案 0 :(得分:1)

由于成绩是与学生实体严格相关的属性,因此逻辑应该放在学生班级内。对于您的示例,设计gradelinq属性中的学生实体可以更容易地阅读和保存代码。 一旦您需要使用成绩属性,您也不会在数据操作操作中污染业务核心逻辑。