[英]Only primitive types ('such as Int32, String, and Guid') are supported in this context
I'm getting the following error:我收到以下错误:
Unable to create a constant value of type 'Phoenix.Intranet.Web.ClientSettings.ComponentRole'.
无法创建类型为“Phoenix.Intranet.Web.ClientSettings.ComponentRole”的常量值。 Only primitive types ('such as Int32, String, and Guid') are supported in this context.
此上下文仅支持原始类型(“例如 Int32、String 和 Guid”)。
I understand why the error occurs.我明白为什么会发生错误。 What I don't understand is why my code is creating the error.
我不明白为什么我的代码会产生错误。 My comparisons are against primitive types.
我的比较是针对原始类型的。 All the comparisons are Guid to Guid.
所有比较都是Guid to Guid。 The error specifically states that Guids are ok.
该错误特别指出Guids没问题。
The error occurs on this line (towards the bottom):错误发生在这一行(朝向底部):
var vla = (from cir in phoenixEntities.ComponentInRoles
Code:代码:
List<ComponentRole> roles;
using (IMSMembershipEntities entities = new IMSMembershipEntities())
{
roles = (from role1 in entities.Roles
select new ComponentRole{Name = role1.RoleName, RoleId = role1.RoleId} ).ToList();
}
List<Components> componentInRoles;
using (PhoenixEntities phoenixEntities = new PhoenixEntities())
{
phoenixEntities.ContextOptions.LazyLoadingEnabled = false;
componentInRoles = (from component in phoenixEntities.Components
select new Components{Name = component.Name,
ComponentId = component.ComponentId,
//InRoles = (from componentInRole in phoenixEntities.ComponentInRoles
// join role in roles on componentInRole.RoleId equals role.RoleId
// where componentInRole.ComponentId == component.ComponentId
// select new ComponentRole{RoleId = role.RoleId, Name = role.Name})
}
).ToList();
foreach (Components cmpent in componentInRoles)
{
Components cmpent1 = cmpent;
//cmpent.InRoles =
var vla = (from cir in phoenixEntities.ComponentInRoles
join role in roles on cir.RoleId equals role.RoleId
where cir.ComponentId == cmpent1.ComponentId
select role).ToList();
}
}
EntityFramework and Linq to SQL both try to translate such queries which a part is in memory and the other part is in Database, to sql IN
operator. EntityFramework and Linq to SQL both try to translate such queries which a part is in memory and the other part is in Database, to sql
IN
operator.
and because your class which roles is an IEnumerable of is not a primitive type it cannot be translated to SQL query.并且因为您的角色是 IEnumerable 的 class 不是原始类型,所以它不能转换为 SQL 查询。
you should first fetch from database to memory and then join two lists in memory.您应该首先从数据库中获取 memory,然后加入 memory 中的两个列表。
for example:例如:
var vla = (from cir in phoenixEntities.ComponentInRoles.ToList()
join role in roles on cir.RoleId equals role.RoleId
where cir.ComponentId == cmpent1.ComponentId
select role).ToList();
I hope I helped我希望我有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.