Reputation: 4811
I have a model for handling score calculation based on different rules
Scoremaster
----------
Grade
MinScore
MaxScore
GradeRule
Some sample data is presented here
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
The algorithm i am planning is like this
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
}
based on the input i can filter to specifica grade category. next step is to detect the score is in which range based on the properties minScore and MaxScore and then return the corresponding Grade and i am struggling to do that using Linq
Upvotes: 2
Views: 494
Reputation: 8385
Try This
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);
}
}
Usage Like this
var scores= db.myTable.FindMinimum("rule").ToList();
Upvotes: 1
Reputation: 1214
Do it like below:
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();
}
Hope it helps.
Upvotes: 2