如何在LINQ中进行左连接?

时间:2014-04-25 23:40:18

标签: vb.net linq ado.net left-join

我有两个具有相同结构的DataTable。这些表ID和LIST中有两列。一个表从内部数据库填充,其他表由对供应商数据库的webservice调用填充。

这两个表都有接近200万条记录。

我试图比较两个表来找出匹配的位置,这样我就可以找出需要同步的记录。

Table 1            Table 2          ACTION
--------           --------         ------
ID  LIST           ID  LIST
--  ----           --  ----
1   ABC            1   ABC          No Change
1   BCD                             Add To Table 2 (1,BCD)
2   ABC            2   ABC          No Change
2                  2   BCD          Delete From Table 2 (2,BCD) Leave (2,ABC) 
                   3   BCD          Delete From Table 2 (3,BCD)

提供数据。我提出了以下LINQ查询

Dim t1_DistinctIDs As System.Collections.Generic.IList(Of String) = (From r In ds.Tables("t1").AsEnumerable
                                                                     Select r.Field(Of String)("ID")).Distinct.ToList

Dim t2_DistinctIDs As System.Collections.Generic.IList(Of String) = (From r In ds.Tables("t2").AsEnumerable
                                                                     Select r.Field(Of String)("ID")).Distinct.ToList

'Records to Delete from Table 2 Because They Don't Exist In Table 1
Dim IDsToDelete As Array = (From r In ds.Tables("t2").AsEnumerable()
                            Where Not t1_DistinctIDs.Contains(r.Field(Of String)("ENTITY_ID"))).ToArray

'Get All ID & LIST in Table 1
Dim t1_AllItems As Array = (From r In ds.Tables("t1").AsEnumerable
                            Select New With
                              {
                                .ID = r.Field(Of String)("ID"),
                                .LIST = r.Field(Of String)("LIST")
                              }
                           ).ToArray

'Find All Records In Table 1 That Do Not Exist In Table 2
Dim ListToAdd As Array = (From r In ds.Tables("t2").AsEnumerable()
                          Join l In t1_AllItems
                            On r.Field(Of String)("ID") Equals l.ID _
                           And r.Field(Of String)("LIST") Equals l.LIST _
                          Select New With
                            {
                              .ID = r.Field(Of String)("ID"),
                              .NAME = r.Field(Of String)("LIST")
                            }
                         ).ToArray

如果我使用T-SQL,这就是我要做的。

DECLARE @T1 AS TABLE (
    ID INT,
    LIST VARCHAR(10)
)

DECLARE @T2 AS TABLE (
    ID INT,
    LIST VARCHAR(10)
)


INSERT INTO @T1(ID,LIST) VALUES (1,'ABC')
INSERT INTO @T1(ID,LIST) VALUES (1,'BCD')
INSERT INTO @T1(ID,LIST) VALUES (2,'ABC')
INSERT INTO @T2(ID,LIST) VALUES (1,'ABC')
INSERT INTO @T2(ID,LIST) VALUES (2,'ABC')
INSERT INTO @T2(ID,LIST) VALUES (2,'BCD')
INSERT INTO @T2(ID,LIST) VALUES (3,'BCD')

SELECT 
    t1.ID,
    t1.LIST
FROM @T1 t1
LEFT JOIN @T2 t2
  ON t1.ID = t2.ID
 AND t1.LIST = t2.LIST
WHERE t2.ID IS NULL

我试图弄清楚如何在LINQ中执行左连接,其中t2.ID IS为空。

0 个答案:

没有答案