简体   繁体   English

使用linq连接来自不同服务器的两个表

[英]Joining two tables from different servers using linq

I'm trying to join two tables from different servers , but it keeps throwing this exception : 我试图将来自不同服务器的两个表联接起来,但是它总是抛出此异常:

This method supports LINQ to Entities infrastructure and is not intended to be used directly from your code. 此方法支持LINQ to Entities基础结构,不能直接在您的代码中使用。

here is my query : 这是我的查询:

 var myList = (from myTableA in _dbProvider.ContextOne.TableA
               join myTableB in _dbProvider.ContextOne.TableB on myTableA.ProductId equals myTableB.Oid
               join myTableC in _dbProvider.ContextTwo.TableC on myTableB.Id equals myTableC.ProductId
               where 
               select  myTableC.Name).Distinct().ToList();

what's that mean ?, knowing that I found an other solution by getting data separately from each table into lists then joining them but it's very greedy in terms of time is there any other solution ? 这是什么意思?知道我找到了另一个解决方案,就是将每个表中的数据分别放入列表中,然后将它们联接起来,但是在时间上却很贪婪,还有其他解决方案吗?

You can't join two tables from two different servers. 您不能将来自两个不同服务器的两个表连接在一起。 Definitely not from EF. 绝对不是来自EF。 Your best bet is to only fetch the data in two separate lists and then join them together using Linq to objects. 最好的选择是只获取两个单独列表中的数据,然后使用Linq将它们连接到对象。

Let me make an imaginary example: You have 1000,000 invoices on one table, each one has about 10 items, a total of 10,000,000 invoice details on anther server. 让我做一个假想的例子:您在一张桌子上有1000,000张发票,每张有大约10项,在另一台服务器上总共有10,000,000张发票明细。 You need Invoices and their details for 10 first invoices created on 2015-5-4 您需要2015年5月4日创建的10张第一张发票的发票及其详细信息

you send a query to first DB, getting only that 10 invoices, extract their id s and use that to query about 100 rows from the other server. 您向第一个数据库发送查询,仅获取10张发票,提取其id然后使用该id查询另一台服务器上的约100行。 This is only about two times slower than making a single join query. 这仅比进行单个联接查询慢大约两倍。

In some cases this becomes impossible (you have conditions on both tables) and you need to bring more rows, but in simple scenarios this is possible. 在某些情况下,这变得不可能(您在两个表上都具有条件​​),并且需要带更多的行,但是在简单的情况下,这是可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM