2017-05-28 86 views
1

學生類的屬性等級/ GradeLinq由Points和Scoring List驅動。最佳實踐Linq驅動屬性

我發現了兩種不同的方法來計算等級。下面顯示爲Grade和GradeLinq

什麼是更好的良好的班級設計實踐。提供dis /優點或更好的解決方案。

的計分類

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

btw .Find和.ForEach是List方法。我沒有看到任何LINQ方法 – Slai

回答

1

由於等級是嚴格相關於邏輯應放置在學生類內的學生實體的一個屬性。舉例來說,設計gradelinq屬性中的學生實體可以更容易閱讀和維護代碼。 一旦您需要使用grade屬性,您也不會污染數據操縱操作中的業務核心邏輯。