0

我正在尝试在 2 个数据表之间执行左连接,这将返回左表中的所有记录,而右表中没有连接条件上的相应值。截至目前,我有以下没有返回任何内容:

Dim Query1 = From exasset In dtExistingAssets _
             GroupJoin asset In dtNewAssets _
             On exasset("ACCOUNT_NAME") Equals asset("ACCOUNT_NAME") _
             Into results = Group _
             From f In results.DefaultIfEmpty _
             Where IsDBNull(f) _
             SelectNewWith _
             { //...

我看过几个关于 using 的引用,Any但我无法正确获取语法。有人可以帮忙吗?这在 SQL 中很容易完成,但在LINQ中似乎要复杂得多。

4

2 回答 2

1

我会使用强类型的DataRow扩展方法,比如Field也支持空值。

Dim query = From exAsset In dtExistingAssets
        Group Join newAsset In dtNewAssets
        On exAsset.Field(Of String)("ACCOUNT_NAME") Equals newAsset.Field(Of String)("ACCOUNT_NAME") Into Group
        From joinedAssets In Group.DefaultIfEmpty()
        Where joinedAssets.Field(Of String)("ACCOUNT_NAME") Is Nothing

如果您只想了解新帐户,还可以使用高效Enumerable.Except

Dim existingAccounts = From exRow In dtExistingAssets
                       Select exRow.Field(Of String)("ACCOUNT_NAME")
Dim newAccounts = From newRow In dtNewAssets
                       Select newRow.Field(Of String)("ACCOUNT_NAME")
Dim newAccNotInExisting = newAccounts.Except( existingAccounts )
于 2012-09-27T15:27:29.590 回答
1

我认为问题在于IsDBNull(f),左连接将导致空值(Nothing在 VB 中)而不是DBNull值。我认为您应该将其更改为:``

...
From f In results.DefaultIfEmpty _
Where f is Nothing
于 2012-09-27T16:48:45.907 回答