'/> '/> 从作用域引用的'Product'类型的变量'x',但未定义 | 所有编程讨论 | zhouni.net

问题 从作用域引用的'Product'类型的变量'x',但未定义


我有一个名为的班级 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();
}

调用 Load 从 BindData 抛出异常:

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);
}

8155
2018-01-12 19:45


起源

看起来像SubSonic中的一个错误。 (@Kobi:不。) - Timwi
@Timwi有什么解决方法吗? - TheVillageIdiot


答案:


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

问题实际上是SubSonic(@Timwi是对的)。这是正确的:

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

我把它改成后:

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

一切都好。


13
2018-01-16 18:37



你知道为什么这是一个问题吗? - mat
不幸的是,这是错误消息的最高点,因为这不是大多数问题的答案,包括我和@ user1039462。对我来说,问题是您必须使用相同的ParameterExpression 到处。它是 不够 ParameterExpression具有相同的变量名称。我相信它是,但我得到关于范围的例外,因为我在表达式树和根LambdaExpression中不是ParameterExpression的同一个实例。 - Will
@问题是在SubSonic中。如果没有明确设置,则不设置变量值。 - TheVillageIdiot
@TheVillageIdiot:我知道,但错误信息是一样的,这个问题是错误信息的最高点:/ - Will
我只是在这里留下这个评论,因为它是错误消息的最高点。如果您正在使用ServiceStack.OrmLite并尝试此异常 Select 来自DB的东西,请注意使用的正确完成空检查 NullableVar != null而不是 NullableVar.HasValue - J0HN