efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Add Include in programmatic way

Open afoliaco opened this issue 11 years ago • 6 comments

I'm not sure if this is the correct place to ask this question, but I look in stackoverflow and in the announcements without any luck. Using the code below Customer and Payments does not get return as part of the order entity.

if there a different way to do multiples include?

// Default includes
var navegation = this.Context.Orders.Include(c => c.OrderItems).ThenInclude(x => x.ShippingMethod).
                                     Include(c=> c.OrderItems).ThenInclude(x=> x.ShipToAdress).
                                     Include(c=> c.ShippingMethod);


if ((include & OrderDetailIncludes.Customer) == OrderDetailIncludes.Customer)
    navegation.Include(c => c.BillToCustomer);

if ((include & OrderDetailIncludes.OrderPaymentMethods) == OrderDetailIncludes.OrderPaymentMethods)
    navegation.Include(c => c.PaymentMethods);

var order = await navegation.FirstOrDefaultAsync(o => o.Id == orderId);

if (order == null)
    throw new Exceptions.GetDetailException($"Order Id {orderId} does not exists");

return order;

afoliaco avatar Sep 08 '15 01:09 afoliaco

@SonOfSam Just like other LINQ operators, the Include method does not mutate the query it is called on, but rather returns a new query with the Include applied. So in your if statements you would need to do something like:

navegation = navegation.Include(c => c.BillToCustomer);

ajcvickers avatar Sep 08 '15 17:09 ajcvickers

@ajcvickers Thanks for the reply, I tried to do that, but when I do this navigation = navigation.Include(c => c.BillToCustomer); I get the following error.

'Cannot implicitly convert type 'Microsoft.Data.Entity.Query.IIncludableQueryable<Orders, Customers>' to 
'Microsoft.Data.Entity.Query.IIncludableQueryable<Order, ShippingMethod>>'. 
An explicit conversion exists (are you missing a cast?)'

Or Something to that extended

afoliaco avatar Sep 08 '15 17:09 afoliaco

@SonOfSam you just need to change you variable type to be IQueryable<Order> rather than using var.

rowanmiller avatar Sep 08 '15 22:09 rowanmiller

@SonOfSam Or if you prefer you might as well call AsQueryable() at the end of the first query to get var to infer the right type, e.g.:

// Default includes
var navegation = this.Context.Orders.Include(c => c.OrderItems).ThenInclude(x => x.ShippingMethod).
                                     Include(c=> c.OrderItems).ThenInclude(x=> x.ShipToAdress).
                                     Include(c=> c.ShippingMethod)
                                     .AsQueryable();

And then do as @ajcvickers suggested.

Notice that a similar problem can occur with OrderBy() as described here: http://stackoverflow.com/questions/1732236/linq2sql-cannot-convert-iqueryable-to-iorderedqueryable-error

divega avatar Sep 08 '15 23:09 divega

@rowanmiller, @divega , @ajcvickers thank you all of you for the help, I was able to resolve the issue with your suggestions, below is the snipped code for future reference.

IQueryable<Order> navigation = this.Context.Orders.Include(c => c.OrderItems).ThenInclude(x => x.ShippingMethod).
                                     Include(c => c.OrderItems).ThenInclude(x => x.ShipToAdress).
                                     Include(c => c.ShippingMethod);


if ((include & OrderDetailIncludes.Customer) == OrderDetailIncludes.Customer)
    navigation = navigation.Include(c => c.BillToCustomer);

if ((include & OrderDetailIncludes.OrderPaymentMethods) == OrderDetailIncludes.OrderPaymentMethods)
    navigation = navigation.Include(c => c.PaymentMethods);

var order = await navigation.FirstOrDefaultAsync(o => o.Number == orderNumber);

Quick question when you I do navigation = navigation.Include(c => c.PaymentMethods); EF is keeping track of the previous include correct?

afoliaco avatar Sep 09 '15 00:09 afoliaco

EF is keeping track of the previous include correct?

It certainly should. The new query should still contain all the Includes previously added. Don't hesitate to create a new issue if you run into unexpected behavior.

divega avatar Sep 09 '15 01:09 divega