如何创建一个BETWEEN条件
本文关键字:一个 BETWEEN 条件 创建 何创建 | 更新日期: 2023-09-27 18:17:30
我有一个模型来处理基于不同规则的分数计算
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
像下面这样做:
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();
}
希望能有所帮助。
试试这个
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();