c# - 在此上下文中仅支持原始类型 ('such as Int32, String, and Guid' )

标签 c# linq-to-entities

我收到以下错误:

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/

相关文章:

Linq-to-SQL EntitySet 不是 IQueryable —— 任何解决方法?

azure - Linq to Entities,当我尝试使用 group .. 时操作超时

c# - RhinoMock 3.6.1 报错方法未调用?

c# - 从\\localhost\xyz 启动时,.NET 可执行文件不会加载引用的程序集

c# - Web API 的缓存策略

c# - LINQ 到实体 : using Contains in the "select" portion throws unexpected error

c# - 使用 Entity Framework 选择一个范围

c# - 将 List<Expression<Func<T, bool>>> 组合到 OR 子句 LINQ

c# - 如果与开关速度

c# - .Net Core Linux 不支持 SecurityIdentifier?