store表达式>作为一个类属性

本文关键字:一个 属性 Func 表达式 store boolean | 更新日期: 2023-09-27 18:16:43

我有一个类扩展了System.Web.UI.WebControls.GridView控件。我想有一个属性,可以保存我的EF表达式在整个控件中使用。

问题是T没有定义

public sealed class NCGridView : GridView
{
    private Expression<Func<T, bool>> _where;
    public void LoadWhere(Expression<Func<T, bool>> where)
    {
        _where = where;
    }
}

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

store表达式<Func<T, boolean >>作为一个类属性

答案二:更新:

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