C# 无法创建类型的常量值 此上下文中仅支持原始类型或枚举类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18929483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to create a constant value of type Only primitive types or enumeration types are supported in this context
提问by user2515186
I am getting this error for the query below
我在下面的查询中收到此错误
Unable to create a constant value of type
API.Models.PersonProtocol
. Only primitive types or enumeration types are supported in this context
无法创建类型为 的常量值
API.Models.PersonProtocol
。此上下文中仅支持原始类型或枚举类型
ppCombined
below is an IEnumerable
object of PersonProtocolType
, which is constructed by concat of 2 PersonProtocol
lists.
ppCombined
下面是 的一个IEnumerable
对象PersonProtocolType
,它由 2 个PersonProtocol
列表的 concat 构造而成。
Why is this failing? Can't we use LINQ JOIN
clause inside of SELECT
of a JOIN
?
为什么这会失败?我们不能JOIN
在SELECT
a 中使用 LINQ子句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
})
});
采纳答案by Slauma
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 afteryou have retrieved the other properties from the database:
这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()
});
回答by Roelant
Don't know if anyone searches for this. I had the same problem. A select on the query and then doing the where (or join) and using the select variable solved the problem for me. (problem was in the collection "Reintegraties" for me)
不知道有没有人搜索这个。我有同样的问题。对查询进行选择,然后执行 where(或连接)并使用 select 变量为我解决了问题。(问题出在我的“重新整合”系列中)
query.Select(zv => new
{
zv,
rId = zv.this.Reintegraties.FirstOrDefault().Id
})
.Where(x => !db.Taken.Any(t => t.HoortBijEntiteitId == x.rId
&& t.HoortBijEntiteitType == EntiteitType.Reintegratie
&& t.Type == TaakType))
.Select(x => x.zv);
hope this helps anyone.
希望这可以帮助任何人。
回答by Colin
In my case, I was able to resolve the issue by doing the following:
就我而言,我能够通过执行以下操作来解决该问题:
I changed my code from this:
我从这个改变了我的代码:
var r2 = db.Instances.Where(x => x.Player1 == inputViewModel.InstanceList.FirstOrDefault().Player2 && x.Player2 == inputViewModel.InstanceList.FirstOrDefault().Player1).ToList();
To this:
对此:
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();
回答by khaled saleh
Just add AsEnumerable() andToList() , so it looks like this
只需添加 AsEnumerable() 和ToList() ,所以它看起来像这样
db.Favorites
.Where(x => x.userId == userId)
.Join(db.Person, x => x.personId, y => y.personId, (x, y).ToList().AsEnumerable()
ToList().AsEnumerable()
回答by James Perih
It's worth adding, since the OP's code sample doesn't provide enough context to prove otherwise, but I received this error as well on the following code:
值得补充的是,因为 OP 的代码示例没有提供足够的上下文来证明否则,但我在以下代码中也收到了此错误:
public RetailSale GetByRefersToRetailSaleId(Int32 refersToRetailSaleId)
{
return GetQueryable()
.FirstOrDefault(x => x.RefersToRetailSaleId.Equals(refersToRetailSaleId));
}
Apparently, I cannot use Int32.Equals
in this context to compare an Int32 with a primitive int; I had to (safely) change to this:
显然,我不能Int32.Equals
在这种情况下使用将 Int32 与原始 int 进行比较;我不得不(安全地)更改为:
public RetailSale GetByRefersToRetailSaleId(Int32 refersToRetailSaleId)
{
return GetQueryable()
.FirstOrDefault(x => x.RefersToRetailSaleId == refersToRetailSaleId);
}