【问题标题】:Join 2 different entities from 2 different models into a single Linq to Entities query将来自 2 个不同模型的 2 个不同实体加入到单个 Linq to Entities 查询中
【发布时间】:2011-01-27 14:23:11
【问题描述】:

我有一个默认的实体框架模型,它包含我的产品的所有默认表,并且所有客户都共享这些表。但是,对于某些客户,我有一些仅为该客户存在的自定义表,但它们与默认产品的表相关。我有第二个实体框架模型来保存这些自定义表。
我的问题是如何使用 Join 进行 Linq to Entities 查询,以便将默认模型中的实体与自定义模型上的表相关联?我不介意没有从自定义实体到默认模型上的实体的导航属性;我只需要一种在单个查询中查询两个模型的方法。
下面是代码:

  using (ProductEntities oProductDB = new ProductEntities())
  {
    using (ProductEntitiesCustom oProductCustomDB = new ProductEntitiesCustom())
    {
      var oConsulta = oProductCustomDB.CTBLCustoms
                .Where(CTBLCustoms => CTBLCustoms.IDWOHD >= 12)
                .Join(oProductDB.TBLResources,
                     CTBLCustoms => new
                       {
                         CTBLCustoms.IDResource
                       },
                     TBLResources => new
                       {
                         TBLResources.IDResource
                       },
                    (CTBLCustoms, TBLResources) => new
                       {
                         IDCustom = CTBLCustoms.IDCustom,
                         Descricao = CTBLCustoms.Descricao,
                         IDWOHD = CTBLCustoms.IDWOHD,
                         IDResource = CTBLCustoms.IDResource,
                         ResourceCode = TBLResources.Code
                       });

      gvwDados.DataSource = oConsulta;
    }
  }

我收到 The specified LINQ expression contains references to queries that are associated with different contexts 错误。
编辑
我可以将 2 ObjectContext 合并为第三个,然后运行 ​​Linq 查询吗? Tks

编辑

以下是使用 AsEnumerable() 建议的解决方案的有效代码:

  using (ProductEntities oProductDB = new ProductEntities())
  {
    using (ProductEntitiesCustom oProductCustomDB = new ProductEntitiesCustom())
    {
      var oConsulta = (oProductCustomDB.CTBLCustoms.AsEnumerable()
                .Where(CTBLCustoms => CTBLCustoms.IDWOHD >= 12)
                .Join(oProductDB.TBLResources,
                     CTBLCustoms => new
                       {
                         CTBLCustoms.IDResource
                       },
                     TBLResources => new
                       {
                         TBLResources.IDResource
                       },
                    (CTBLCustoms, TBLResources) => new
                       {
                         IDCustom = CTBLCustoms.IDCustom,
                         Descricao = CTBLCustoms.Descricao,
                         IDWOHD = CTBLCustoms.IDWOHD,
                         IDResource = CTBLCustoms.IDResource,
                         ResourceCode = TBLResources.Code
                       })).ToList();

      gvwDados.DataSource = oConsulta;
    }
  }

我按照建议添加了AsEnumerable(),但我必须在末尾添加ToList(),以便将其数据绑定到DataGridView。

【问题讨论】:

    标签: entity-framework entity-framework-4 linq-to-entities


    【解决方案1】:

    你不能在 L2E 中做到这一点。您可以使用AsEnumerable() 将其带入对象空间,它会起作用,但可能效率低下。

    合并 ObjectContexts 是可能的,并且会起作用,但需要手动完成。

    【讨论】:

    • Tks。您是否有一些链接,以便我可以获得有关您建议的方法的更多信息?太感谢了
    猜你喜欢
    • 1970-01-01
    • 2015-11-07
    • 2013-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多