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

问题描述

我收到以下错误:

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')在这种情况下,支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 17:52