user1231231412
user1231231412

Reputation: 1659

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

Upvotes: 2

Views: 1066

Answers (1)

Reza ArabQaeni
Reza ArabQaeni

Reputation: 4908

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

Upvotes: 2

Related Questions