Search code examples
c#asp.netsqllinqentity-framework

Error Message: "Only primitive types or enumeration types are supported in this context."


I'm working a report for an asp.net page and I'm trying to assign the results of this linq statement to an existing dataset but I'm receiving the "Only primitive types or enumeration types are supported in this context." Error message. Here's my code:

   var contextTable = new dbpersonnelEntities();                
   var contextHistory = new WriterInResidenceEntities();

   var rslt = (from c in contextTable.TableofOrganizationRpts
            where !(from o in contextHistory.emp_history
            select o.employeeID).Contains((int)c.employeeid_fk)
                       select c).ToList();

   ReportDataSource r = new ReportDataSource("tableOfOrgDS", rslt);

Solution

  • You can't mix entity types from different contexts in single LINQ query. Either put them into single context, or try to execute this:

    var employeeIds = (from o in contextHistory.emp_history select o.employeeID).ToList();
    

    and then pass this list into the second query:

    var rslt = (from c in contextTable.TableofOrganizationRpts
                where !employeeIds.Contains((int)c.employeeid_fk)
                select c).ToList();
    

    This should generate IN condition in resulting SQL query. Note, that this might work slow.