2

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;
    }
user1231231412
  • 1,659
  • 2
  • 26
  • 42
  • Well... What is T meant to represent? Would storing is as a non-generic Expression suffice? (casting it when needed and known) – Marc Gravell Dec 04 '11 at 22:00
  • T is the type of the row bound to your GridView. Can you know this at design time? I've never heard of a generic control. No, wait, look at [this](http://stackoverflow.com/questions/1329362/c-sharp-generics-usercontrol). – sq33G Dec 04 '11 at 22:00
  • 1
    Can you show an example of how you intend to use the expression? – dtb Dec 04 '11 at 22:01
  • @Marc ha I sneaked in my edit before the deadline – sq33G Dec 04 '11 at 22:03
  • @dtb My overall goal is to have an extended GridView control that will work with my POCO entities. So I can tell the gridview which entity set I'm working with and it will know how to work with them internally. – user1231231412 Dec 04 '11 at 22:21

1 Answers1

2

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();
Reza ArabQaeni
  • 4,848
  • 27
  • 46