存储表达式< Func< T,bool>.在哪里作为阶级财产
问题描述:
我有一个扩展System.Web.UI.WebControls.GridView控件的类.我想拥有一个可以保存我的EF表达式的属性,以便在整个控件中使用.
I have a class that extends the System.Web.UI.WebControls.GridView control. I want to have a property that can save my EF expression to use throughout the control.
问题是,T没有定义.
public sealed class NCGridView : GridView
{
private Expression<Func<T, bool>> _where;
public void LoadWhere(Expression<Func<T, bool>> where)
{
_where = where;
}
}
RedHat建议尝试
RedHat suggestion attempt
private Expression<Func<BaseModel, bool>> _where;
public void LoadWhere<T>(Expression<Func<T, bool>> where) where T : BaseModel
{
// Cannot cast from: Expression<Func<T, bool>> to: Expression<Func<BaseModel, bool>>
_where = where;
}
答
答案2:更新:
public Expression<Func<BaseModel, bool>> LoadWhere<T>(Expression<Func<T, bool>> where) where T : BaseModel
{
where = LambdaExpression.Lambda<Func<BaseModel, bool>>(where.Body,where.Parameters);
}
答案1:使用:
Expression<Func<object, bool>>
支持任何类型.
示例:
Expression<Func<object, bool>> exp = p => ((Table1)p).Code == 1;
var a = new MyContext().Table1.Where(exp).ToList();