2011-12-04 19 views
1

我有一個類擴展了System.Web.UI.WebControls.GridView控件。 我想要一個屬性,可以保存我的EF表達式以在整個控件中使用。商店Expression <Func <T, bool>>其中作爲類屬性

問題是,T沒有定義。

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

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

紅帽建議嘗試

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

嗯......什麼爲T意味着代表什麼?將存儲作爲非通用表達式足夠嗎? (在需要時將其轉換並知道) –

+0

T是綁定到GridView的行的類型。你能在設計時知道嗎?我從來沒有聽說過一種通用控制。不,等等,看看[這個](http://stackoverflow.com/questions/1329362/c-sharp-generics-usercontrol)。 – sq33G

+1

你能舉一個你打算如何使用表達式的例子嗎? – dtb

回答

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

EF –

+0

可能無法正常工作我對它進行了測試,將添加一些示例。 –

+0

@RedHat,我認爲在這種情況下使用泛型類型vs使用基類是我的問題。 – user1231231412

相關問題