错误消息:“在此上下文中仅支持基本类型或枚举类型。”

时间:2013-05-30 12:42:20

标签: c# asp.net sql linq entity-framework

我正在为asp.net页面编写报告,我正在尝试将此linq语句的结果分配给现有数据集,但我收到“在此上下文中仅支持原始类型或枚举类型“。错误信息。这是我的代码:

   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);

1 个答案:

答案 0 :(得分:0)

您不能在单个LINQ查询中混合来自不同上下文的实体类型。要么将它们放在单个上下文中,要么尝试执行它:

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

然后将此列表传递给第二个查询:

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

这应该在生成的SQL查询中生成IN条件。请注意,这可能会很慢。