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

TheVillageIdiot picture TheVillageIdiot · Jan 12, 2011 · Viewed 10.2k times · Source

I have a class named Product in class library project. I am using SubSonic SimpleRepository to persist objects. I have a method as follows in Product class:

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

I'm calling this function like this:

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

Calling Load from BindData throws the exception:

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

How can I resolve this.

EDIT:- by stepping through SubSonic code I found that the error is thrown by this function

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

Answer

TheVillageIdiot picture TheVillageIdiot · Jan 16, 2011

After banging my head on the wall for many days and even asking Jon Skeet for help, I found out the problem.

The problem actually is with SubSonic (@Timwi was right). It is right in this line:

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

After I changed it to:

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

all was well.