[英]store Expression<Func<T, bool>> where as a class property
I have a class that extends the System.Web.UI.WebControls.GridView control. 我有一个扩展System.Web.UI.WebControls.GridView控件的类。 I want to have a property that can save my EF expression to use throughout the control. 我想拥有一个可以保存我的EF表达式以在整个控件中使用的属性。
Problem is, T is not defined. 问题是,T没有定义。
public sealed class NCGridView : GridView
{
private Expression<Func<T, bool>> _where;
public void LoadWhere(Expression<Func<T, bool>> where)
{
_where = where;
}
}
RedHat suggestion attempt RedHat建议尝试
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;
}
Answer 2: Update: 答案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);
}
Answer 1: Use: 答案1:使用:
Expression<Func<object, bool>>
Supported any type. 支持任何类型。
Sample: 样品:
Expression<Func<object, bool>> exp = p => ((Table1)p).Code == 1;
var a = new MyContext().Table1.Where(exp).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.