我收到以下错误:
无法创建常量值 类型 'Phoenix.Intranet.Web.ClientSettings.ComponentRole'。 只有原始类型('如Int32, 支持String和Guid') 这个背景。
我理解错误发生的原因。我不明白的是我的代码创建错误的原因。我的比较是针对原始类型的。所有的比较都是Guid to 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();
}
}
答案 0 :(得分:18)
EntityFramework和Linq to SQL都试图将部分在内存中而另一部分在数据库中的此类查询转换为sql IN
运算符。
并且因为你的类哪个角色是IEnumerable而不是原始类型,所以它无法转换为SQL查询。
首先应该从数据库获取到内存,然后在内存中加入两个列表。
例如:
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();
我希望我帮助