无法创建类型的常量值 此上下文中仅支持原始类型或枚举类型 [英] Unable to create a constant value of type Only primitive types or enumeration types are supported in this context
问题描述
我在下面的查询中收到此错误
I am getting this error for the query below
无法创建 API.Models.PersonProtocol
类型的常量值.此上下文中仅支持原始类型或枚举类型
Unable to create a constant value of type
API.Models.PersonProtocol
. Only primitive types or enumeration types are supported in this context
下面的
ppCombined
是一个PersonProtocolType
的IEnumerable
对象,由2个PersonProtocol
列表的concat构成.
ppCombined
below is an IEnumerable
object of PersonProtocolType
, which is constructed by concat of 2 PersonProtocol
lists.
为什么会失败?我们不能在 JOIN
的 SELECT
内使用 LINQ JOIN
子句吗?
Why is this failing? Can't we use LINQ JOIN
clause inside of SELECT
of a 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
是内存中的对象集合,您不能将数据库中的一组数据与另一组数据连接起来内存中的数据.您可以尝试在内存中提取 ppCombined
集合的过滤项 personProtocol
从数据库中检索其他属性:>
This cannot work because ppCombined
is a collection of objects in memory and you cannot join a set of data in the database with another set of data that is in memory. You can try instead to extract the filtered items personProtocol
of the ppCombined
collection in memory after you have retrieved the other properties from the database:
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()
});
这篇关于无法创建类型的常量值 此上下文中仅支持原始类型或枚举类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!