0

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 Answer 1

1

Just chain them together:

var result = _rep.GetCustomers();
if (useByCompanyId)
  _rep = _rep.ByCompanyID(companyId);
if (useByCountry)
  _rep = _rep.ByCountry(county);
Sign up to request clarification or add additional context in comments.

3 Comments

_rep doesnt have any filters, just those methods like GetCustomers()
_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!
yep, that works. IQueryable<Customers> result = _rep.GetCustomers(); did the trick. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.