vb.net LinQ - 左连接两个数据表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13272721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LinQ - Left Join Two DataTables
提问by Brian
I am wondering if anyone can help me. I am new to LINQ and still trying to understand how it fits together.
我想知道是否有人可以帮助我。我是 LINQ 的新手,仍然试图了解它是如何组合在一起的。
I have a simple left join query in SQL, returning all transactions that are on table 1 that do not exist in table 2. Both tables are identical in structure.
我在 SQL 中有一个简单的左连接查询,返回表 1 中不存在于表 2 中的所有事务。两个表的结构相同。
SELECT Table1.*
FROM Table1 LEFT JOIN Table2 ON Table1.DealReference = Table2.DealReference
WHERE (((Table2.DealReference) Is Null));
Can someone please guide me, how to achieve the same in LinQ.
有人可以指导我,如何在 LinQ 中实现相同的目标。
I am using the following DataTables:
我正在使用以下数据表:
Dim currentDataTable = _DataTable1.AsEnumerable
Dim previousDataTable = _DataTable2.AsEnumerable
I am looking to have the results output back into a datatable.
我希望将结果输出回数据表。
Thanks
谢谢
BM
BM
回答by Tim Schmelter
1) Linq-To-DataTableis a subset of Linq-To-Objectsand assumes that the data is already in memory. So if you want a scalable solution you might want to have a look at Linq-To-Sqlwhich queries the database directly.
1)Linq-To-DataTable是一个子集,Linq-To-Objects并假设数据已经在内存中。因此,如果您想要一个可扩展的解决方案,您可能需要查看Linq-To-Sql哪些直接查询数据库。
2) Your join is not a simple left join but a left-outer join. You can achieve that for example with this query:
2)您的联接不是简单的左联接,而是左外联接。例如,您可以使用以下查询来实现:
Dim rows = From r1 In _DataTable1
Group Join r2 In _DataTable2
On r1.Field(Of String)("DealReference") Equals r2.Field(Of String)("DealReference")
Into DataGroup = Group
From row In DataGroup.DefaultIfEmpty
Where row Is Nothing
Select r1
Dim tblresult = rows.CopyToDataTable()

