gpt4 book ai didi

c# - 用于重复比较的谓词表达式函数

转载 作者:行者123 更新时间:2023-11-30 17:10:53 24 4
gpt4 key购买 nike

我有一个测试近 200 个案例的谓词处理程序,每个测试涉及五个可能的比较。我想简化这段代码,但在如何从语法上表达这一点上遇到了瓶颈。

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;

上面的 switch 语句重复了将近 200 次,每次唯一不同的是检查的字段名称。我想尽可能地减少这段代码。

最佳答案

您可以像这样动态构建表达式:

string propertyName = GetPropertyName(f);
ExpressionType comp = GetComparisonType(f);
ParameterExpression p = Expression.Parameter(typeof(OADataConsolidated));
Expression<Func<OADataConsolidated, bool>> expr =
Expression.Lambda<Func<OADataConsolidated, bool>>(
Expression.MakeBinary(
comp,
Expression.Property(p, propertyName),
Expression.Constant((double)value)),
p);

predicate = predicate.And(expr);


...

static string GetPropertyName(DOTFilter filter)
{
switch(filter.IdValue)
{
case 4: // GED: Reasoning
propertyName = "ajblGEDR_Mean";
break;
case 5: // GED: Mathematics
propertyName = "ajblGEDM";
break;
...
default:
throw new ArgumentException("Unknown Id value");
}
}

static ExpressionType GetComparisonType(DOTFilter filter)
{
switch (filter.Comp)
{
case DOTFilter.Comparitor.LessThan:
return ExpressionType.LessThan;
case DOTFilter.Comparitor.EqualOrLess:
return ExpressionType.LessThanOrEqual;
case DOTFilter.Comparitor.EqualTo:
return ExpressionType.Equal;
case DOTFilter.Comparitor.EqualOrGreater:
return ExpressionType.GreaterThanOrEqual;
case DOTFilter.Comparitor.GreaterThan:
return ExpressionType.GreaterThan;
default:
throw new ArgumentException("Unknown Comp value");
}
}

开关仍然存在,但不再重复。

关于c# - 用于重复比较的谓词表达式函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11730694/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com