2010-08-26 59 views
0

我有許多擴展 「過濾器」,如這一個:實體框架,延伸加入 「過濾器」

public static IQueryable<Customer> ByCustomerID(this IQueryable<Customer> qry, int customerID) 
{ 
    return from c in qry 
      where c.CustomerID == customerID 
      select c; 
} 

要GetCustomers的()(IQueryable的),如.ByCompanyID()等等等等,並且我想根據標準添加這些過濾器。

有點像:

var result = _rep.GetCustomers(); 

if(useByCompanyID) 
    // add .ByCompanyID(companyID) to "result" 
if(useByCountry) 
    // add .ByCountry(country) to "result" 

// etc etc.... 

//do something with "result" 

是可以使用實體框架和LINQ辦?

/M

回答

1

只是把它們連在一起:

var result = _rep.GetCustomers(); 
if (useByCompanyId) 
    _rep = _rep.ByCompanyID(companyId); 
if (useByCountry) 
    _rep = _rep.ByCountry(county); 
+0

_rep可是沒有任何過濾器,只是那些方法,如GetCustomers的() – 2010-08-26 11:29:21

+0

_rep被聲明爲IQueryable的?然後它有任何方法decalred作爲擴展方法(這個IQueryable )。如果_rep不是IQueryable 那麼它需要! – amaca 2010-08-26 11:35:52

+0

是的,那是有效的。 IQueryable result = _rep.GetCustomers();訣竅。謝謝 – 2010-08-26 11:50:12