store Expression <func <t,bool =“”>&gt;其中作为类属性</func <t,>

时间:2011-12-04 21:58:58

标签: c# asp.net .net entity-framework

我有一个扩展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;
    }

1 个答案:

答案 0 :(得分:1)

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