我在下面的查询中遇到此错误
无法创建类型的常量值API.Models.PersonProtocol。在此上下文中仅支持原始类型或枚举类型
API.Models.PersonProtocol
ppCombined下面是的IEnumerable对象PersonProtocolType,由2个PersonProtocol列表的concat构造。
ppCombined
IEnumerable
PersonProtocolType
PersonProtocol
为什么会失败?我们不能JOIN在SELECTa 内部使用LINQ 子句JOIN吗?
JOIN
SELECT
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 }) });
这ppCombined是行不通的,因为它是内存中的一组对象,并且您无法将数据库中的一组数据与内存中的另一组数据连接在一起。你可以尝试,而不是提取经过滤项personProtocol中的ppCombined集合中的内存 后, 您检索从数据库中其他属性:
personProtocol
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() });