将来自 2 个不同模型的 2 个不同实体连接到单个 Linq to Entities 查询中

发布于 2024-10-14 10:55:06 字数 2758 浏览 1 评论 0原文

我有一个默认的实体框架模型,其中包含我的产品的所有默认表,并且所有客户都共享该模型。但是,对于某些客户,我有一些仅针对该客户存在的自定义表,但它们与默认产品的表相关。我有第二个实体框架模型来保存这些自定义表。
我的问题是如何使用 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;
    }
  }

我收到指定的 LINQ 表达式包含对与不同上下文关联的查询的引用错误。
编辑
我可以将 2 个 ObjectContext 合并到第三个中,然后运行 ​​Linq 查询吗? 谢谢

编辑

下面是使用 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。

I have a default Entity Framework model that holds all of my default tables for my product, and that all customers share in common. However, on some customers, I have some custom tables that exist for only that customer, but they relate to the default product's tables. I have a second Entity Framework model to hold these custom tables.
My question is how can I make a Linq to Entities query using Join so I can relate the entities from my default model to the tables on my custom model? I don't mind not having the Navigation properties from the custom entity to the entities on the default model; I just need a way to query both models in a single query.
Below is the code:

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

I get a The specified LINQ expression contains references to queries that are associated with different contexts error.
EDIT
Could I merge the 2 ObjectContext into a third, and then run the Linq query?
Tks

EDIT

Below is the code that worked, using the AsEnumerable() proposed solution:

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

I added the AsEnumerable() as suggested, but I had to add the ToList() at the end so I could databind it to the DataGridView.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谎言月老 2024-10-21 10:55:06

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

合并 ObjectContext 是可能的,并且可以工作,但需要手动完成。

You can't do this in L2E. You could bring this into object space with AsEnumerable(), and it would work, but possibly be inefficient.

Merging the ObjectContexts is possible, and would work, but would need to be done manually.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文