2017-05-15 38 views
2

我想我的抽象電話IQueryable,這樣我可以通過這裏進入功能:如何將右側「where」傳遞給c#中的IQueryable實例化?

喜歡的東西:

public IQueryable<T> executeQuery<T>(T baseType, Expression<Func<T,object>> whereFunc) where T : class 
{ 
    //Get context 
    DataContext dbContext = new DataContext(connection); 

    //Get the table representation 
    Table<T> baseTable = dbContext.GetTable<T>(); 

    //Get our query object 
    IQueryable<T> baseQuery = from item in baseTable where whereFunc select item; 
} 

以上不工作,但有一些方法來做到這一點? (即一般創建IQueryable,但允許傳入where子句?)

回答

2

您的whereFunc簽名是錯誤的。 Where是一個過濾器,因此應該返回bool,而不是object。另外,在這種情況下,您不能使用簡化的linq語法。這應該工作:

public IQueryable<T> executeQuery<T>(T baseType, Expression<Func<T,bool>> whereFunc) where T : class 
{ 
    //Get context 
    DataContext dbContext = new DataContext(connection); 

    //Get the table representation 
    Table<T> baseTable = dbContext.GetTable<T>(); 

    //Get our query object 
    IQueryable<T> baseQuery = baseTable.Where(whereFunc); 
} 
相關問題