I have a many to many relationship in EF Code First between Contacts and Lists. ProxyCreation and LazyLoading are disabled to allow serialization of the entities.
I have a query that is meant to return contacts that are in a given list.
// GET api/Contacts
[Queryable]
public IQueryable<Contact> GetContacts(int bulkListId)
{
var bulkList = db.BulkLists.Include(c => c.Contacts).Where(c => c.ID == bulkListId).SingleOrDefault();
if (bulkList == null)
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
return bulkList.Contacts.AsQueryable().OrderBy(c => c.ID).Include(c => c.AddressBookType).Include(c => c.BulkLists);
}
Although this works, it doesn’t work as intended. It results in the correct set of contacts who are in a given list but these contacts only have that list populated in their Lists property of the relationship. So when this is serialized and gets back to the client it hides the other lists that the contacts are members of.
I can’t see how the query is filtering it in this way and how I might change it to include the full set of lists. Any advice would be very much appreciated.
You’re cheating! 🙂
By adding
AsQueryable()tobulkList.Contactsyou make it possible to continue withIncludewithout making the compiler complain. BUT…As per MSDN on DbExtensions.Include:
(emphasis mine)
And
EntityCollectiondoes not have anIncludemethod, so nothing happens.You’ll have to expand the include list in your first statement, probably like so: