If I select from an IQueryable then the Include is lost

Posted by Connor Murphy on Stack Overflow See other posts from Stack Overflow or by Connor Murphy
Published on 2010-05-27T19:49:57Z Indexed on 2010/05/27 19:51 UTC
Read the original article Hit count: 350

The include does not work after I perform a select on the IQueryable query. Is there a way arround this? My query is

public IQueryable<Network> GetAllNetworks()
{
    var query = (from n in _db.NetworkSet
                .Include("NetworkContacts.Contact")
                .Include("NetworkContacts.Contact.RelationshipSource.Target")
                .Include("NetworkContacts.Contact.RelationshipSource.Source")
                 select (n));        
    return query;;
}

I then try to populate mya ViewModel in my WebUI layer using the following code

            var projectedNetworks =
            from n in GetAllNetworks()
            select new NetworkViewModel
            {
                Name = n.Name,
                Contacts = from contact in networkList
                    .SelectMany(nc => nc.NetworkContacts)
                    .Where(nc => nc.Member == true)
                    .Where(nc => nc.NetworkId == n.ID)
                    .Select(c => c.Contact)                        
                    select contact,
            };

        return projectedNetworks;

The problem now occurs in my newly createdNetworkViewModel The Contacts object does include any loaded data for RelationshipSource.Target or RelationshipSource.Source

The data should is there when run from the original Repository IQueryable object. However the related include data does not seem to get transferred into the new Contacts collection that is created from this IQueryable using the Select New {} code above. Is there a way to preserve this Include data when it gets passed into a new object?

© Stack Overflow or respective owner

Related posts about entity-framework

Related posts about linq-to-entities