LINQ Query: Dynamic Add Includes at Run Time

I have a GetOrder (int OrderID) method that launches a LINQ query and returns an order. There are many properties of an object that can be preloaded (i.e. with Include ()) in a request, for example. DeliveryMethod, Customer, CustomerBillingAddress, etc. I want the method to allow the caller to specify which properties are preloaded using the method arguments. Needless to say, I don't want to explicitly write out every possible LINQ query in this method.

Thus, you can program the main query and then add. Turns on dynamically, for example.

if(PreLoadCustomer)     query.Include("Customer") 

Note. I know that there is lazy loading - this will not work - related objects should be available immediately.

thanks

+2
source share
1 answer

So, you can program the main request, and then add. Turns on dynamically

In fact, it would be better to add Includes first, and then write the request itself:

 ObjectQuery<Order> orders = db.Orders; if(PreLoadCustomer) orders = orders.Include("Customer"); var query = from o in orders ... 
+5
source

All Articles