Question

I have the following code in my BLL which is accessed via a WCF service call:

public List<Dispatch> GetDispatchesByDateRange(DateTime start, DateTime end, params string[] includes)
{
    MyEntities entities = new MyEntities();
    var res = from d in entities.Dispatches
              where d.Route.Legs.Any(x =>
                  x.StartPoint.ArrivalTime >= start && x.StartPoint.ArrivalTime <= end ||
                  x.StartPoint.DepartureTime >= start && x.StartPoint.DepartureTime <= end ||
                  x.EndPoint.ArrivalTime >= start && x.EndPoint.ArrivalTime <= end ||
                  x.EndPoint.DepartureTime >= start && x.EndPoint.DepartureTime <= end)
              select d;

    ObjectQuery<Dispatch> query = res as ObjectQuery<Dispatch>;

    foreach (string s in includes)
        query.Include(s);

    return query.ToList();
}

One of the calls from the client side sends a few includes along to eager load related entities. The problem I'm running into is that the Includes are being ignored. I've read that EF will ignore includes when used in a subquery or as part of a projection. I'm not doing either of those in this case, just selecting the entire entity based on a where condition and then appending the includes. If I don't use a where condition, the includes come across just fine. Has anyone else run into this situation where simply adding a where condition causes includes to be ignored? Could it be because my 'where' digs down into the relationship hierarchy too far?

Was it helpful?

Solution 2

Finally got this working, had to use this formulation:

ObjectQuery<Dispatch> query = (from item in entities.Dispatches select item) as ObjectQuery<Dispatch>;

            foreach (string s in includes)
                query = query.Include(s);

            var res = from d in query
                      where d.Route.Legs.Any(x =>
                      x.StartPoint.ArrivalTime >= start && x.StartPoint.ArrivalTime <= end ||
                      x.StartPoint.DepartureTime >= start && x.StartPoint.DepartureTime <= end ||
                      x.EndPoint.ArrivalTime >= start && x.EndPoint.ArrivalTime <= end ||
                      x.EndPoint.DepartureTime >= start && x.EndPoint.DepartureTime <= end)
                      select d;

Essentially, the main difference is that I'm doing an initial linq query, then attaching the includes and finally reselecting from that group with the where condition.

OTHER TIPS

You can try to call Include extension methods before Where().

In EF 5 it can be done as

DbQuery<Dispatch> query = entities.Dispatches;

foreach (var include in includes)
{
    query = query.Include(include);
}

var res = from d in dispatches 
          where ...
          select d;

I had the same issue, EF ignoring Include() for no reason. I worked around it by using a subquery. Not the best solution, but couldn't find another working one... Snippet below. Edit: You could probably replace this by a join as well, didn't have time to test it.

Note: I'm using EF Core RC1 at time of writing (not my decision).

var result = projs.Select(p => new FoundProject
                {
                    ProjectId = p.Id,
                    ProjectName = p.Name,
                    ProjectEntityTypeId = p.ProjectEntityTypeId,
                    ProjectEntityType = context.ProjectEntityTypes.Where(e => e.Id == p.ProjectEntityTypeId).Select(e => e.Name).FirstOrDefault(),
                    RentStatus = p.RentStatus,
                    RentPrice = p.RentPrice
                });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top