加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 18:09:36 所属栏目:百科 来源:网络整理
导读:我收到以下错误: 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. 我明白为什么错误发生.我不明白的是为
我收到以下错误:

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();

我希望我能帮忙

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读