c# – 在此上下文中仅支持基本类型(如Int32,String和Guid)

前端之家收集整理的这篇文章主要介绍了c# – 在此上下文中仅支持基本类型(如Int32,String和Guid)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我收到以下错误

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 to Guid.该错误明确指出,圭ids是好的.

该行发生错误(向下):

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操作符,这些查询是内存中的一部分,另一部分在数据库中.

并且因为你的类哪个角色是一个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();

我希望我能帮忙

猜你在找的C#相关文章