简体   繁体   English

Linq to Sql:从DB1-table1中仅选择DB2-table2上不存在的项目

[英]Linq to Sql: Select only items from DB1-table1 that don't exist on DB2-table2

I'have been working around on how to properly implement the bellow task in ac# project. 我一直在研究如何在ac#项目中正确实现波纹管任务。

It is pretendended to ... 它假装成...

Get all data that exists in a particular database's table(db1) but that does NOT EXISTS on another particular database's table (db2) 获取存在于特定数据库的表(db1)中但不存在于另一个特定数据库的表(db2)上的所有数据

both tables have common ids 两个表都有共同的ID

I've faced lots of posts about this but it seems none solves my problem. 我已经遇到过很多关于此的帖子,但似乎没有一个可以解决我的问题。 Any help? 有什么帮助吗?

EDITED: 编辑:

Select all data 
on table_x from database_x 
Where item_id from table_x are not found inside table_y from database_y

=> return data in list format =>以列表格式返回数据

This were the solution that I was looking for. 这是我一直在寻找的解决方案。 Based on @user1949706's answer I selected all data from both tables with LINQ (also from different databases) and I placed it on memory. 基于@ user1949706的回答,我使用LINQ从两个表中选择了所有数据(也来自不同的数据库),并将其存储在内存中。

To fully answer my question on how to do this with LINQ here it is: 为了完全回答我关于如何使用LINQ做到这一点的问题,这里是:

//DB1
db1DataContext db1 = new db1DataContext();
//DB2
db2DataContext db2 = new db2DataContext();


//SELECT ALL DATA FROM DB1
var result1 = (from e in db1.Items
               select e
              ).ToList();

//SELECT ALL DATA FROM DB2
var result2 = (from e in db2.Item2s
               select e
              ).ToList();

//SELECT ALL ELEMENTS FROM DB2.TABLE THAT DO NOT EXISTS ON DB1.TABLE BASED ON EXISTING ID's            
var resultFinal = ( from e in result1
                    where !(from m in result2
                            select m.Id).Contains(e.Id)
                    select e
                  ).ToList();

I would also thank Robert Rouse on his anwser to this question and everybody else who tried to help. 我还要感谢罗伯特·劳斯(Robert Rouse)对这个问题的回答,以及所有其他想帮助的人。

Hope it helps someone else! 希望它能帮助别人!

According to this answer you can join tables over different databases with LINQ. 根据答案,您可以使用LINQ连接不同数据库上的表。 Even join accross servers. 甚至跨服务器加入。

Another option is to read all the data you need in memory and join both tables in memory (you can still use LINQ for that). 另一个选择是读取内存中所需的所有数据,然后将内存中的两个表连接起来(您仍然可以使用LINQ)。

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

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