views:

240

answers:

1

The following query fails to load the tables when I execute it:

IEnumerable<Bookmark> tempBookmarks = ListBookmarksByUserID(userID);
IEnumerable<CandidateWithBookmarks> results = (from c in _internshipEntities.CandidateSet
                                            .Include("education")
                                            .Include("progress")
                                            .Include("contacts")
                                            .Include("availability")
                                            .Include("hosttypes")
                                            .Include("hostsizes")
                                            .Include("hostcapacities")
                                            .Include("hoststates")
                                            .Include("users")
                       join b in tempBookmarks on c.ID equals b.candidates.ID
                       select new CandidateWithBookmarks()
                                  {CandidateObject = c, BookmarkObject = b});
return results;

I have found some articles related to the problem, namely Alex James' article "How to make Include really Include". The solution comes with one caveat:

For this to work your final select must be entities, i.e. select post rather than select new {…}

Which is obviously an issue for the above block of code. Are there any other known work-arounds for this issue that won't break eager loading?

+2  A: 

I think I solved this but it might only work for this specific instance, by moving the includes after the join, the query appears to work:

IEnumerable<CandidateWithBookmarks> results = (
    from b in tempBookmarks
    join c in _internshipEntities.CandidateSet
                                 .Include("education")
                                 .Include("progress")
                                 .Include("contacts")
                                 .Include("availability")
                                 .Include("hosttypes")
                                 .Include("hostsizes")
                                 .Include("hostcapacities")
                                 .Include("hoststates")
                                 .Include("users")
    on b.candidates.ID equals c.ID
    select new CandidateWithBookmarks(){CandidateObject = c, BookmarkObject = b});

Edit: Another query I have similar to this requires an outer join as well, which creates some issues since it then matters what you join to what, unlike this example, but it's still doable.

Graham