The*_*iot 11 c# lambda subsonic3 linq-expressions
我有一个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();
}
Run Code Online (Sandbox Code Playgroud)
我正在调用这个函数:
private void BindData()
{
var list = Product.Load(x => x.Active);//Active is of type bool
rptrItems.DataSource = list;
rptrItems.DataBind();
}
Run Code Online (Sandbox Code Playgroud)
调用Load
从BindData
引发异常:
variable 'x' of type 'Product' referenced from scope '', but it is not defined
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题.
编辑: - 通过单步执行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);
}
Run Code Online (Sandbox Code Playgroud)
The*_*iot 13
在我的头撞墙多日,甚至向Jon Skeet寻求帮助后,我发现了问题所在.
问题实际上是SubSonic(@Timwi是对的).这是正确的:
var list = Product.Load(x => x.Active);//Active is of type bool
Run Code Online (Sandbox Code Playgroud)
我把它改成后:
var list = Product.Load(x => x.Active==true);
Run Code Online (Sandbox Code Playgroud)
一切都好.
归档时间: |
|
查看次数: |
9284 次 |
最近记录: |