Entity Framework select on IQueryable throwing "must be reducible node"

c# entity-framework entity-framework-core navigation-properties

Question

I have this query where predicate is Expression<Func<Property, bool>> and the whole query returns an IQueryable:

var query = _db.PropertyRepository.Get(predicate)
               .Include(x => x.Info)
               .ThenInclude(x => x.Address)
               .Include(x => x.TransactionListingAgents)
               .ThenInclude(x => x.Agent)
               .ThenInclude(x => x.Person)
               .ThenInclude(x => x.Contact)

I'd like to perform a select on that IQueryable to project to a DTO, because I need to do some data manipulation with that data. So I have this other query

query.Select(x => new BasePropertyDTO() {
    Id = x.Id,
    StreetNumber = x.Info.Address.StreetNumber,
    Street = x.Info.Address.StreetName,
});

However, it throws an exception

must be reducible node

My workaround was doing ToList() before the Select() and that doesn't throw the exception anymore , but now I'm grabbing data that I don't need.

I have found out that the issue comes when I try to project on my select properties from Info navigation property or any navigation property, even though I have the Include() calls.

Any ideas on why this is behaving this way or what's wrong with my queries?

Complete query after upgrading to 1.1.0:

query.ToList().Select(x => new BasePropertyDTO() {
                Id = x.Id,
                StreetNumber = (x.Info != null && x.Info.Address != null) ? x.Info.Address.StreetNumber : "",
                Street = (x.Info != null && x.Info.Address != null) ? x.Info.Address.StreetName : "",
                City = (x.Info != null && x.Info.Address != null) ? x.Info.Address.City : "",
                AgentName = x.TransactionListingAgents.Where(t => t.Agent != null && t.Agent.Person != null && t.Agent.Person.Contact != null && t.ListingId == x.Id && t.BrokerageId == x.BrokerageId).Select(a => a.Agent.Person.Contact.FullName).FirstOrDefault()
            });

The AgentName part throws

An item with the same key has already been added

If I remove that, I still get the

must be reducible node

1
2
11/22/2016 4:54:24 PM

Popular Answer

EF team has fixed this issue on Entity Framework Core 1.1.So you have to use that version to avoid the above issue.

Entity Framework Core 1.1

Git Issue which was solved on the above version :

'must be reducible node' when aggregating over a join

1
11/21/2016 11:47:21 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