2011-01-12 31 views
11

我在類庫項目中有一個名爲Product的類。我正在使用SubSonic SimpleRepository來堅持對象。我有一個方法,在Product類如下:從範圍引用的'Product'類型的變量'x',但沒有定義

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

調用從BindDataLoad拋出該異常:

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

看起來像是在SubSonic中的錯誤。 (@Kobi:No.) – Timwi 2011-01-12 19:53:46

+0

@Timwi有什麼解決方法嗎? – TheVillageIdiot 2011-01-13 03:43:29

回答

13

拋出敲打牆壁上我的頭了很多天,甚至要求喬恩斯基特求助後,我發現了問題。

這個問題實際上是與SubSonic(@Timwi是對的)。正確的做法是在這條線:

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

後,我把它改爲:

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

一切都很好。

相關問題