The Include is gone if I choose from an IQueryable.

anonymous-types entity-framework include linq-to-entities

Question

The include not operate when I do a choose on theIQueryable query. Exists a method to get around this? My question 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 attempt to use the following code to fill my ViewModel in my WebUI layer.

            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 issue now appears in my just constructedNetworkViewModel ; the Contacts No loaded data is present in collection objects forRelationshipSource.Target or RelationshipSource.Source .

However, the data is there when the original Repository is used.IQueryable method. But the connectedinclude It seems that data is not moved to the newContacts collection produced while using theSelect New NetworkViewModel {} .

Is it possible to keep this?Include information when it is transferred into a new object? Currently, I simply keep receivingNull exceptions

1
9
6/18/2019 4:47:58 AM

Accepted Answer

Here is a description of what is occurring and a workaround..

However, I believe there is a better better solution in your situation than what is described in the linked article. Currently, you're using a view model forNetwork . That's great! Your eager loading issues will suddenly disappear if you apply it to contact (and its associated attributes, too). It works to project always.

7
5/28/2010 12:58:49 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow