Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only primitive types ('such as Int32, String, and Guid') are supported in this context

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();
    }
}
like image 858
Chuck Conway Avatar asked Jun 30 '11 18:06

Chuck Conway


1 Answers

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

like image 61
Maziar Taheri Avatar answered Oct 14 '22 08:10

Maziar Taheri