views:

20

answers:

1

Hello

I have lots of extended "filters" such as this one:

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), such as .ByCompanyID() etc etc, and I would like to add those filters depending on criterias.

Kinda like:

var result = _rep.GetCustomers();

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

 // etc etc....

 //do something with "result"

is that possible to do using the entity framework and linq?

/M

+1  A: 

Just chain them together:

var result = _rep.GetCustomers();
if (useByCompanyId)
  _rep = _rep.ByCompanyID(companyId);
if (useByCountry)
  _rep = _rep.ByCountry(county);
amaca
_rep doesnt have any filters, just those methods like GetCustomers()
molgan
_rep is declared as being IQueryable<Customer> ? Then it does have any methods decalred as being extension methods with (this IQueryable<Customer>). And if _rep isn't of IQueryable<Customer> then it needs to be!
amaca
yep, that works. IQueryable<Customers> result = _rep.GetCustomers(); did the trick. Thanks
molgan