从范围“”引用了 'System.Data.DataRow' 类型的变量“”,但未定义

Variable '' of type 'System.Data.DataRow' referenced from scope '', but it is not defined

我正在尝试构建通用函数以在 DataRows.

上应用规则

但是我在 运行 运行程序时出现以下错误

这是我的代码。

public Func<T, bool> CompileRuleDataRow<T>(Rule r)
{
    var paramUser = Expression.Parameter(typeof(T));
    Expression expr = BuildExprDataRow<T>(r, paramUser);
    // build a lambda function User->bool and compile it
    //Expression.Lambda<Func<T, bool>>(
    return Expression.Lambda<Func<T, bool>>(expr, paramUser).Compile();
}

构建表达式的函数。

public Expression BuildExprDataRow<T>(Rule r, ParameterExpression param)
{
    ParameterExpression objExpr = Expression.Parameter(typeof(string));
    string defaultMember = "Item";
    ConstantExpression indexExpr = Expression.Constant(r.MemberName);
    Expression leftIndexAccessExpr = Expression.Property(objExpr, defaultMember, indexExpr);
    ExpressionType tBinary;
    // is the operator a known .NET operator?
    ExpressionType.TryParse(r.Operator, out tBinary);
    var right = Expression.Constant(r.TargetValue);
    // use a binary operation, e.g. 'Equal' -> 'u.Age == 15'
    return Expression.MakeBinary(tBinary, leftIndexAccessExpr, right);
}

我在main方法中就是这样调用的

var rule = new Rule("Name", "Equal", "3");
Func<DataRow, bool> compiledRuleDataRow = CompileRuleDataRow<DataRow>(rule);
DataTable dt = new DataTable();
dt.Columns.Add("Id");
dt.Columns.Add("Name");
dt.Columns.Add("Address");
for (int i = 0; i < 100000; i++)
{
  dt.Rows.Add(i.ToString(), i.ToString(), i.ToString());
}
//I want to do something like this.
    DataRow[] drFiltered = dt.Select().Where(r => compiledRuleDataRow(r)).ToArray();

下面是我的Ruleclass

public class Rule
{
  public string MemberName { get; set; }
  public string Operator { get; set; }
  public string TargetValue { get; set; }

  public Rule(string MemberName, string Operator, string TargetValue)
  {
    this.MemberName = MemberName;
    this.Operator = Operator;
    this.TargetValue = TargetValue;
  }
}

当我 运行 此代码时出现以下错误。

variable '' of type 'System.Data.DataRow' referenced from scope '', but it is not defined

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: variable '' of type 'System.Data.DataRow' referenced from scope '', but it is not defined

有人可以帮帮我吗?

与@svick 一样,我收到了与发布的代码不同的错误。

无论如何,BuildExprDataRow 方法有两个问题。首先,它没有使用传递的 param 参数。其次,属性 值未正确转换,因此二元运算符不起作用。

固定方法如下:

public Expression BuildExprDataRow<T>(Rule r, ParameterExpression param)
{
    var right = Expression.Constant(r.TargetValue);
    var left = Expression.Convert(
        Expression.Property(param, "Item", Expression.Constant(r.MemberName)),
        right.Type);
    var comparison = (ExpressionType)Enum.Parse(typeof(ExpressionType), r.Operator);
    return Expression.MakeBinary(comparison, left, right);
}

顺便说一下,泛型参数 T 也没有在该方法中使用,因此您可以将其删除(使该方法成为非泛型)。