简体   繁体   中英

store Expression<Func<T, bool>> where as a class property

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.

Problem is, T is not defined.

public sealed class NCGridView : GridView
{
    private Expression<Func<T, bool>> _where;

    public void LoadWhere(Expression<Func<T, bool>> where)
    {
        _where = where;
    }
}

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

Answer 2: Update:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM