我在下面的查询中收到此错误
无法创建类型的常量值API.Models.PersonProtocol。此上下文仅支持原始类型或枚举类型
API.Models.PersonProtocol
ppCombined下面是 的一个IEnumerable对象,它是由 2 个列表PersonProtocolType的 concat 构造的。PersonProtocol
ppCombined
IEnumerable
PersonProtocolType
PersonProtocol
为什么会失败?我们不能在aJOIN中使用 LINQ 子句吗?SELECT``JOIN
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 }) });
这是行不通的,因为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() });