.Execute not available on a type safe Entity Framework query (Include & where)

161 views Asked by At

I have a question regarding entity framework queries.

I have a query which is using the Extension methods provided by EF, so i can use type safe includes and where clause. But as the Include is with a lambda parameter it is an extension method on IQueryable that returns an IQueryable in order to chain methods like Where. Include with a string parameter is a method on ObjectQuery that returns an ObjectQuery. Execute is a method on ObjectQuery, not IQueryable so it is not available when you use IQueryable methods.

Is there a way to call .Execute but with IQueryable?

    return
    this.Storage.Customer.OfType<Preferred>()
    .Include(b  => b.Order)
    .Where(cust => cust.Id == customerId && cust.CustomerType== (int)cusType)
    .SingleOrDefault();

Thanks,

1

There are 1 answers

0
MicroMan On

I have found it, maybe someone can comment if this is good practice or not?

        var query =             
            this.Storage.Tubes.OfType<Preferred>()
                        .Include(b  => b.Order);

        return ((ObjectQuery<Preferred>)query).Execute(MergeOption.OverwriteChanges)
                                               .SingleOrDefault(cust => cust.Id == customerId && cust.CustomerType == (int)cusType);