I have two tables, Orders and Customers, with Orders referencing Customers, and their mapped classes Order and Customer.
I want to write a method which receives a Linq Where() condition for both Orders and Customers:
void ProcessOrders(Expression<Func<Order, bool>> orderCondition,
Expression<Func<Customer, bool>> customerCondition)
{
var q = Database.Query<Order>().Where(orderCondition);
q = q.Where(o => o.Customer APPLY customerCondition); // how ?
... process records in q ...
}
Even after reading lots of SO answers on this topic and related articles on MSDN, it seems I cannot figure out how to apply the customer condition on the Order.Customer property.
(DB/ORM in use is Oracle/DevExpress, but I’m fine with a generic solution, so I did not include them in the tags. Please mention if your answer is restricted to a specific DB or ORM)
This is one way to achieve it.
Again, depending on the ORM, something like this may also work (I’m assuming a lazily loaded Customer.Orders collection property here):
Any solution has to be tested against the specific linq provider, since different linq providers may or may not support certain queries…