Vb.net Linq到对象
我有两个来自不同服务器的数据库。我无法链接数据库。从数据库中检索数据的方式如下: DB1 -客户ID参与\U ID启用说明Vb.net Linq到对象,vb.net,linq-to-objects,Vb.net,Linq To Objects,我有两个来自不同服务器的数据库。我无法链接数据库。从数据库中检索数据的方式如下: DB1 -客户ID参与\U ID启用说明 600 10真正的公司1 600 20假公司2 700 10真正的公司3 DB2 已启用客户端\u ID参与\u ID 说明 600 5真正的公司1 600 10假公司2 500 30真正的公司3 此任务的T SQL为: select * from DB1 left join DB2 on DB1.client_ID = DB2.client_ID and DB1.
- 600 10真正的公司1
- 600 20假公司2
- 700 10真正的公司3
- 已启用客户端\u ID参与\u ID 说明
- 600 5真正的公司1
- 600 10假公司2
- 500 30真正的公司3
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)
我需要执行此操作VB.NET LINQ您提供的示例数据无论如何都不会返回任何值,因为它们的ClientId和EngagementId都有值 我将LINQ分为两个列表。我还没有对此进行测试或优化,但也许这就是你想要的,至少可以让你开始 以下是我的尝试:
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
我修改了sunpech的代码,列表2包含了预期的结果,即DB1-600的第二行,20,False,“Company2”请不要在标题中包含像“VB.NET”这样的标记。请把它们放在标签上。谢谢你的回复。我的SQL正常工作,它返回来自DB1-600 20 False Company2的第二条记录,因为DB2中存在客户机id 600,而DB2中不存在约定id 20。我尝试了你的代码,但它什么也没有返回。这是因为您对列表1使用了联接。我们需要列表1的左联接。我正在尝试将联接转换为左联接。再次感谢。我更改了LINQ查询,结果正常。谢谢你的帮助。
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