只有原始类型('如的Int32,String和的Guid')在这种情况下,支持 [英] Only primitive types ('such as Int32, String, and Guid') are supported in this context

查看:87
本文介绍了只有原始类型('如的Int32,String和的Guid')在这种情况下,支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误:


  

无法创建一个恒定值
  类型
  Phoenix.Intranet.Web.ClientSettings.ComponentRole。
  只有原始类型('如的Int32,
  串,和的Guid')在支持
  这样的背景下。


我理解为什么发生错误。我不明白的是,为什么我的code为创建错误。我比较反对原始类型。所有的比较是GUID来GUID。错误明确规定,GUID是好的。

这条线上出现的错误(对底部):

  VAR VLA =(CIR从在phoenixEntities.ComponentInRoles

code:

 列表< ComponentRole>角色;
使用(IMSMembershipEntities实体=新IMSMembershipEntities())
{
    角色=(从entities.Roles基于role1
             选择新ComponentRole {名称= role1.RoleName,角色ID = role1.RoleId}).ToList();
}清单<成分> componentInRoles;使用(PhoenixEntities phoenixEntities =新PhoenixEntities())
{
    phoenixEntities.ContextOptions.LazyLoadingEnabled = FALSE;
    componentInRoles =(从phoenixEntities.Components部件
                        选择新组件{名称= component.Name,
                                              COMPONENTID = component.ComponentId,
                                              // InRoles =(从componentInRole在phoenixEntities.ComponentInRoles
                                              //加入角色的角色上componentInRole.RoleId等于role.RoleId
                                              //其中componentInRole.ComponentId == component.ComponentId
                                              //选择新ComponentRole {角色ID = role.RoleId,名称= role.Name})
                                              }
                       ).ToList();
    的foreach(在componentInRoles组件cmpent)
    {
        组件cmpent1 = cmpent;
        //cmpent.InRoles =         变种VLA =(从phoenixEntities.ComponentInRoles CIR
                     加入角色的角色上cir.RoleId等于role.RoleId
                     其中,cir.ComponentId == cmpent1.ComponentId
                         选择角色).ToList();
    }
}


解决方案

的EntityFramework和LINQ to SQL都试图翻译这样的查询这部分是在内存中,而另一部分是数据库,为sql IN 运营商。

和因为你的类角色是IEnumerable的不是原始的类型不能转换为SQL查询。

您应该首先从数据库读取到内存中,然后加入两个列表在内存中。

例如:

  VAR VLA =(从phoenixEntities.ComponentInRoles.ToList CIR()
                     加入角色的角色上cir.RoleId等于role.RoleId
                     其中,cir.ComponentId == cmpent1.ComponentId
                         选择角色).ToList();

我希望我帮助

I'm getting the following error:

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.

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. The error specifically states that Guids are ok.

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.

and because your class which roles is an IEnumerable of is not a primitive type it cannot be translated to SQL query.

you should first fetch from database to memory and then join two lists in 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

这篇关于只有原始类型('如的Int32,String和的Guid')在这种情况下,支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆