我有这个查询,其中谓词是 Expression<Func<Property, bool>>
整个查询返回 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)
我想对 IQueryable
执行选择投影到 DTO,因为我需要对该数据进行一些数据操作。所以我有另一个查询
query.Select(x => new BasePropertyDTO() {
Id = x.Id,
StreetNumber = x.Info.Address.StreetNumber,
Street = x.Info.Address.StreetName,
});
但是,它抛出一个异常
must be reducible node
我的解决方法是做 ToList()
在 Select()
之前这不再引发异常,但现在我正在获取不需要的数据。
我发现当我尝试从 Info 导航属性或任何导航属性投影到我的选择属性时,问题就出现了,即使我有 Include()
电话。
关于为什么会这样或者我的查询有什么问题有什么想法吗?
升级到 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()
});
AgentName 部分抛出
An item with the same key has already been added
如果我删除它,我仍然会得到 p>
must be reducible node
最佳答案
EF 团队已在 Entity Framework Core 1.1 上修复了此问题。因此您必须使用该版本才能避免上述问题。
Git 问题 已在上述版本中解决:
关于c# - Entity Framework 选择 IQueryable 抛出 "must be reducible node",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40730981/