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
EF team has fixed this issue on Entity Framework Core 1.1.So you have to use that version to avoid the above issue.
Git Issue which was solved on the above version :