11

我有一个Product在类库项目中命名的类。我SubSonic SimpleRepository用来持久化对象。我在课堂上有一个方法如下Product

public static IList<Product> Load(Expression<Func<Product, bool>> expression)
{
    var rep=RepoHelper.GetRepo("ConStr");
    var products = rep.Find(expression);
    return products.ToList();
}

我这样调用这个函数:

private void BindData()
{
    var list = Product.Load(x => x.Active);//Active is of type bool
    rptrItems.DataSource = list;
    rptrItems.DataBind();
}

调用LoadfromBindData抛出异常:

variable 'x' of type 'Product' referenced from scope '', but it is not defined

我该如何解决这个问题。

编辑:-通过单步SubSonic执行代码,我发现此函数引发了错误

private static Expression Evaluate(Expression e)
{
    if(e.NodeType == ExpressionType.Constant)
        return e;
    Type type = e.Type;
    if(type.IsValueType)
        e = Expression.Convert(e, typeof(object));
    Expression<Func<object>> lambda = Expression.Lambda<Func<object>>(e);
    Func<object> fn = lambda.Compile(); //THIS THROWS EXCEPTION
    return Expression.Constant(fn(), type);
}
4

1 回答 1

14

在把我的头撞在墙上很多天甚至向 Jon Skeet 寻求帮助之后,我发现了问题所在。

问题实际上在于 SubSonic(@Timwi 是对的)。就在这一行:

var list = Product.Load(x => x.Active);//Active is of type bool

在我将其更改为:

var list = Product.Load(x => x.Active==true);

一切都好。

于 2011-01-16T18:37:53.717 回答