无法创建类型的常量值此上下文仅支持基元类型或枚举类型我在下面的查询中收到此错误无法创建类型的常量值API.Models.PersonProtocol。在此上下文中仅支持基元类型或枚举类型ppCombined下面是一个IEnumerable对象PersonProtocolType,由2个PersonProtocol列表的concat构成。为什么这会失败?我们不能使用LINQ JOIN条款里面SELECT的JOIN?var persons = db.Favorites
.Where(x => x.userId == userId)
.Join(db.Person, x => x.personId, y => y.personId, (x, y) =>
new PersonDTO
{
personId = y.personId,
addressId = y.addressId,
favoriteId = x.favoriteId,
personProtocol = (ICollection<PersonProtocol>) ppCombined .Where(a => a.personId == x.personId)
.Select( b => new PersonProtocol()
{
personProtocolId = b.personProtocolId,
activateDt = b.activateDt,
personId = b.personId })
});
3 回答
繁星淼淼
TA贡献1775条经验 获得超11个赞
这不起作用,因为它ppCombined
是内存中对象的集合,您无法使用内存中的另一组数据将数据库中的一组数据连接起来。你可以尝试,而不是提取经过滤项personProtocol
中的ppCombined
集合中的内存后,您检索从数据库中其他属性:
var persons = db.Favorites .Where(f => f.userId == userId) .Join(db.Person, f => f.personId, p => p.personId, (f, p) => new // anonymous object { personId = p.personId, addressId = p.addressId, favoriteId = f.favoriteId, }) .AsEnumerable() // database query ends here, the rest is a query in memory .Select(x => new PersonDTO { personId = x.personId, addressId = x.addressId, favoriteId = x.favoriteId, personProtocol = ppCombined .Where(p => p.personId == x.personId) .Select(p => new PersonProtocol { personProtocolId = p.personProtocolId, activateDt = p.activateDt, personId = p.personId }) .ToList() });
小唯快跑啊
TA贡献1863条经验 获得超2个赞
就我而言,我能够通过执行以下操作来解决问题:
我改变了我的代码:
var r2 = db.Instances.Where(x => x.Player1 == inputViewModel.InstanceList.FirstOrDefault().Player2 && x.Player2 == inputViewModel.InstanceList.FirstOrDefault().Player1).ToList();
对此:
var p1 = inputViewModel.InstanceList.FirstOrDefault().Player1;
var p2 = inputViewModel.InstanceList.FirstOrDefault().Player2;
var r1 = db.Instances.Where(x => x.Player1 == p1 && x.Player2 == p2).ToList();
- 3 回答
- 0 关注
- 1942 浏览
添加回答
举报
0/150
提交
取消