I have a predicate handler that tests almost 200 cases, and each test involves five possible comparisons. I want to streamline this code, but am hitting a wall with how to express this syntactically.
public static Expression<Func<OADataConsolidated, bool>> Match(DOTSearchFilter filters)
{
var predicate = (filters.OrFilters.Count > 0) ? PredicateBuilder.False<OADataConsolidated>() : PredicateBuilder.True<OADataConsolidated>();
foreach (DOTFilter f in filters.AndFilters)
{
int value = -1;
int.TryParse(f.TextValue, out value);
switch (f.Type)
{
case DOTFilter.FilterType.SCO:
switch (f.IdValue)
{
case 4: // GED: Reasoning
switch (f.Comp)
{
case DOTFilter.Comparitor.LessThan:
predicate = predicate.And(p => p.ajblGEDR_Mean < value);
break;
case DOTFilter.Comparitor.EqualOrLess:
predicate = predicate.And(p => p.ajblGEDR_Mean <= value);
break;
case DOTFilter.Comparitor.EqualTo:
predicate = predicate.And(p => p.ajblGEDR_Mean == value);
break;
case DOTFilter.Comparitor.EqualOrGreater:
predicate = predicate.And(p => p.ajblGEDR_Mean >= value);
break;
case DOTFilter.Comparitor.GreaterThan:
predicate = predicate.And(p => p.ajblGEDR_Mean > value);
break;
}
break;
case 5: // GED: Mathematics
switch (f.Comp)
{
case DOTFilter.Comparitor.LessThan:
predicate = predicate.And(p => p.ajblGEDM < value);
break;
case DOTFilter.Comparitor.EqualOrLess:
predicate = predicate.And(p => p.ajblGEDM <= value);
break;
case DOTFilter.Comparitor.EqualTo:
predicate = predicate.And(p => p.ajblGEDM == value);
break;
case DOTFilter.Comparitor.EqualOrGreater:
predicate = predicate.And(p => p.ajblGEDM >= value);
break;
case DOTFilter.Comparitor.GreaterThan:
predicate = predicate.And(p => p.ajblGEDM > value);
break;
}
break;
The above switch statement is repeated almost 200 times and the only thing different in each case is the field name being checked. I want to reduce this code as much as possible.
value
-- TryParse is guaranteed to set the value no matter what; if it returns false, it will have setvalue
to 0. In other words,value
will be equal to -1 only iff.TextValue
parses to -1. And why aren't you checking the return value ofTryParse
? – phoog