2015-10-14 80 views
2

我有基於不同的規則處理分值的計算模型LINQ如何使BETWEEN條件

Scoremaster 
    ---------- 
    Grade 
    MinScore 
    MaxScore 
    GradeRule 

一些樣本數據在這裏

A1 4.5 5  5 
    A2 4 4.5 5 
    B1 3.5 4  5 
    B2 3 3.5 5 
    C 0 3  5 
    A1 8 10 10 
    A2 6 8  10 
    B1 5 6  10 
    C 0 5  10 
    A1 16 20 20 
    A2 12 16 20 
    B1 8 12 20 
    C 0 8  20 

提出的算法我計劃是這樣的

string FindGrade(double score,string rule) 
{ 

List<Scoremaster> scores=(from p in dbcontext.Scoremasters 
          where p.GradeRule==rule 
          select p).ToList(); 

//how can i check the score input of the function belongs to which category from this list using Linq 

//example FindGrade(3,5) = B2 FindGrade(4,5) = A2 
//example FindGrade(7,10) = A2 FindGrade(4,10) = C 
//example FindGrade(17,20) = A1 FindGrade(10,20) = B1        
} 

基於輸入我可以過濾到特定等級類別。下一步是探測得分,其中,基於性能MinScore是和MaxScore,然後返回相應的成績,我努力做到這一點使用LINQ

+0

爲什麼不使用''&&運營商與'minscore'? 'p.GradeRule == rule &&(score => p.MinScore && score <= p.Maxscore)' –

+0

如果你想完全實現'between' [這裏是Jon Skeet的實現](http:// stackoverflow.com/questions/1447635/linq-between-operator) –

回答

2

做到像以下範圍:

string FindGrade(double score,string rule) 
{ 
    List<Scoremaster> scores = (from p in dbcontext.Scoremasters 
         where p.GradeRule==rule && score>= p.MinScore && score<= p.MaxScore 
         select p).ToList(); 
} 

希望它有助於。

1

嘗試這個

public static class Extensions 
{ 
    public IQueryable<T> FindMinimum<T>(this IQueryable<T> source,string rule) 
     where T : YourEntityType 
    { 
     return source.Where(p => p.GradeRule==rule && score>= p.MinScore && score<= p.MaxScore); 
    } 
} 

,像這樣的

var scores= db.myTable.FindMinimum("rule").ToList();