我收到以下错误:
Unable to create a constant value of type 'Phoenix.Intranet.Web.ClientSettings.ComponentRole'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.
我明白为什么会出现这个错误。我不明白的是为什么我的代码会产生错误。我的比较是针对原始类型的。所有的比较都是 Guid 到 Guid。该错误明确指出 Guids 正常。
错误发生在这一行(靠近底部):
var vla = (from cir in phoenixEntities.ComponentInRoles
代码:
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 和 Linq to SQL 都尝试将这样的查询(一部分在内存中,另一部分在数据库中)转换为 SQL IN
运算符。
并且因为您的类 which roles is an IEnumerable of is not a primitive type it cannot be translated to SQL query.
你应该先从数据库中获取到内存,然后在内存中连接两个列表。
例如:
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();
希望对你有帮助
关于c# - 在此上下文中仅支持原始类型 ('such as Int32, String, and Guid' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6539237/