实体框架,扩展添加“过滤器”

时间:2010-08-26 09:00:17

标签: linq entity-framework

我有很多扩展的“过滤器”,比如这个:

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

To 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 个答案:

答案 0 :(得分:1)

将它们连在一起:

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