问题
I have 2 databases from different servers. I cannot link the databases. The data is retrieved from the databases as :
DB1
- Client_ID Engagement_ID Enabled Description
- 600 10 True Company1
- 600 20 False Company2
- 700 10 True Company3
DB2
Client_ID Engagement_ID Enabled Description
600 5 True Company1
600 10 False Company2
500 30 True Company3
The T SQL for this task is:
select * from DB1
left join DB2 on DB1.client_ID = DB2.client_ID
and DB1.Engagement_ID = DB2.Engagement_ID
where DB2.CLient_ID is null
and DB2.Engagement_ID is null and DB1.client_id in (select client_id from DB2)
I need to do this VB.NET LINQ
回答1:
The sample data you provided won't return any values anyway since they ClientId and EngagementId all have values.
I split the LINQ into two lists. I have yet to test this out or optimize it, but maybe this is what you're looking for to at least get you started.
Here is my attempt:
Public Class DBObject
Public Sub New(ByVal cId As Integer, _
ByVal eId As Integer, _
ByVal enabled As Boolean, _
ByRef desc As String)
_clientId = cId
_engagementId = eId
_enabled = enabled
_description = desc
End Sub
Private _clientId As Integer
Public Property ClientId() As Integer
Get
Return _clientId
End Get
Set(ByVal value As Integer)
_clientId = value
End Set
End Property
Private _engagementId As Integer
Public Property EngagementId() As Integer
Get
Return _engagementId
End Get
Set(ByVal value As Integer)
_engagementId = value
End Set
End Property
Private _enabled As Boolean
Public Property Enabled() As Boolean
Get
Return _enabled
End Get
Set(ByVal value As Boolean)
_enabled = value
End Set
End Property
Private _description As String
Public Property Description() As String
Get
Return _description
End Get
Set(ByVal value As String)
_description = value
End Set
End Property
End Class
Dim DB1 As New List(Of DBObject)
Dim DB2 As New List(Of DBObject)
DB1.Add(New DBObject(600, 10, True, "Company1"))
DB1.Add(New DBObject(600, 20, False, "Company2"))
DB1.Add(New DBObject(700, 10, True, "Company3"))
DB2.Add(New DBObject(600, 5, True, "Company1"))
DB2.Add(New DBObject(600, 10, False, "Company2"))
DB2.Add(New DBObject(500, 30, True, "Company3"))
Dim list1 As List(Of DBObject) = (From obj1 As DBObject In DB1 _
Join obj2 As DBObject In DB2 _
On obj1.ClientId Equals obj2.ClientId _
And obj1.EngagementId Equals obj2.EngagementId _
Where obj2.ClientId = Nothing _
And obj2.EngagementId = Nothing _
Select obj1).ToList
Dim list2 As List(Of DBObject) = (From obj3 As DBObject In list1 _
From obj4 As DBObject In DB2 _
Where obj3.ClientId = obj4.ClientId _
Select obj3).ToList
' list2 would have the results you desire
回答2:
Dim list1 = From obj1 As DBObject In DB1 _
Group Join obj2 As DBObject In DB2 _
On obj1.ClientId Equals obj2.ClientId _
And obj1.EngagementId Equals obj2.EngagementId _
Into g = Group _
From r In g.DefaultIfEmpty() _
Where g.ElementAtOrDefault(0) Is Nothing _
Select New With {.Cl_ID = obj1.ClientId, .EngID = bj1.EngagementId}
Dim list2 = (From obj3 In list1 _
From obj4 In DB2 _
Where obj3.Cl_ID = obj4.ClientId _
Select obj3).Distinct.DefaultIfEmpty
I modified sunpech's code and List2 contains the expected result i.e. the second row of DB1 - 600, 20, False, "Company2"
来源:https://stackoverflow.com/questions/3189631/linq-to-objects