使用VB.NET在VS2019中Linq到SQL-外部左连接?
我正在努力从linq到SQL查询中获得所需的结果。我可以找到所有person表记录,或者只找到具有相应date_表条目的记录。这是我正在使用的查询,结果只返回MK编号101和104。我想是因为他们今天没有有效的记录而错过了100号和105号吧使用VB.NET在VS2019中Linq到SQL-外部左连接?,vb.net,linq-to-sql,visual-studio-2019,Vb.net,Linq To Sql,Visual Studio 2019,我正在努力从linq到SQL查询中获得所需的结果。我可以找到所有person表记录,或者只找到具有相应date_表条目的记录。这是我正在使用的查询,结果只返回MK编号101和104。我想是因为他们今天没有有效的记录而错过了100号和105号吧 其中Person\u data\u table.MK=Date\u table.FK和Date\u table.Allocated“休假”和Date\u table.Allocated“生病”和Date\u table.Date=Today和Person\
其中Person\u data\u table.MK=Date\u table.FK和Date\u table.Allocated“休假”和Date\u table.Allocated“生病”和Date\u table.Date=Today和Person\u data\u table.Team=“A”
这是表格数据(注意今天是20年3月15日)-请将答案保存在VB中,因为C#答案在我翻译后不会起作用谢谢你的帮助
问候
彼得
请注意,您也在此处发布了您的问题。 你的问题还没有解决吗?您可以按如下方式尝试我的代码。很抱歉,我没有使用SQL,我使用了类,但这并不影响结果
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim MK100 As NewPerson = New NewPerson With {
.MK = "100",
.First_Name = "Mike",
.Last_Name = "Smith",
.Team = "A"
}
Dim MK101 As NewPerson = New NewPerson With {
.MK = "101",
.First_Name = "Dave",
.Last_Name = "Sharp",
.Team = "A"
}
Dim MK102 As NewPerson = New NewPerson With {
.MK = "102",
.First_Name = "Roger",
.Last_Name = "Hands",
.Team = "A"
}
Dim MK103 As NewPerson = New NewPerson With {
.MK = "103",
.First_Name = "Keely",
.Last_Name = "Riller",
.Team = "A"
}
Dim MK104 As NewPerson = New NewPerson With {
.MK = "104",
.First_Name = "Simon",
.Last_Name = "Says",
.Team = "A"
}
Dim MK105 As NewPerson = New NewPerson With {
.MK = "105",
.First_Name = "Eugene",
.Last_Name = "Phillips",
.Team = "A"
}
Dim MK106 As NewPerson = New NewPerson With {
.MK = "106",
.First_Name = "William",
.Last_Name = "Nobody",
.Team = "B"
}
Dim FK101 As NewDate = New NewDate With {
.FK = "101",
.Date_ = "15-Mar-20",
.Allocated = "Job Number 1"
}
Dim FK102 As NewDate = New NewDate With {
.FK = "102",
.Date_ = "15-Mar-20",
.Allocated = "Vacation"
}
Dim FK103 As NewDate = New NewDate With {
.FK = "103",
.Date_ = "15-Mar-20",
.Allocated = "Sick"
}
Dim FK104 As NewDate = New NewDate With {
.FK = "104",
.Date_ = "15-Mar-20",
.Allocated = "Job nunber 2"
}
Dim Person_Data_table As List(Of NewPerson) = New List(Of NewPerson) From {
MK100,
MK101,
MK102,
MK103,
MK104,
MK105,
MK106
}
Dim Date_Table As List(Of NewDate) = New List(Of NewDate) From {
FK101,
FK102,
FK103,
FK104
}
Dim query = From NewPerson In Person_Data_table Group Join NewDate In Date_Table On NewPerson.MK Equals NewDate.FK Into gj = Group From subperson In gj.DefaultIfEmpty() Where (NewPerson.Team = "A") AndAlso (subperson Is Nothing OrElse ((subperson.Allocated <> "Vacation") AndAlso (subperson.Allocated <> "Sick"))) Select New With {NewPerson.MK, NewPerson.First_Name, NewPerson.Last_Name, NewPerson.Team
}
For Each v In query
ListBox1.Items.Add($"{v.MK,-15}{v.First_Name,-15}{v.Last_Name,-15}{v.Team}")
Next
End Sub
End Class
Class NewPerson
Public Property MK As String
Public Property First_Name As String
Public Property Last_Name As String
Public Property Team As String
End Class
Class NewDate
Public Property FK As String
Public Property Date_ As String
Public Property Allocated As String
End Class
公共类表单1
私有子表单1_Load(发送方作为对象,e作为事件参数)处理MyBase.Load
Dim MK100作为新人=新新人{
.MK=“100”,
.First_Name=“Mike”,
.Last_Name=“Smith”,
.Team=“A”
}
Dim MK101作为新人=新新人{
.MK=“101”,
.First_Name=“Dave”,
.Last_Name=“Sharp”,
.Team=“A”
}
Dim MK102作为新人=新新人{
.MK=“102”,
.First_Name=“罗杰”,
.Last_Name=“Hands”,
.Team=“A”
}
Dim MK103作为新人=新新人{
.MK=“103”,
.First_Name=“Keely”,
.Last_Name=“Riller”,
.Team=“A”
}
Dim MK104作为新人=新新人{
.MK=“104”,
.First_Name=“Simon”,
.Last_Name=“说”,
.Team=“A”
}
尺寸MK105为新人=新新人{
.MK=“105”,
.First_Name=“Eugene”,
.Last_Name=“Phillips”,
.Team=“A”
}
尺寸MK106为新人=新新人{
.MK=“106”,
.First_Name=“William”,
.Last_Name=“无人”,
.Team=“B”
}
Dim FK101 As NewDate=带有{
.FK=“101”,
.Date=“20年3月15日”,
.Allocated=“作业编号1”
}
Dim FK102 As NewDate=带有{
.FK=“102”,
.Date=“20年3月15日”,
.Allocated=“假期”
}
Dim FK103 As NewDate=带有{
.FK=“103”,
.Date=“20年3月15日”,
.Allocated=“生病”
}
Dim FK104 As NewDate=带有{
.FK=“104”,
.Date=“20年3月15日”,
.Allocated=“作业编号2”
}
Dim Person_Data_表格作为列表(新人员)=来自的新列表(新人员){
MK100,
MK101,
MK102,
MK103,
MK104,
MK105,
MK106
}
Dim Date_表作为列表(属于NewDate)=新列表(属于NewDate)来自{
FK101,
FK102,
FK103,
FK104
}
Dim query=From NewPerson In Person_Data_table Group将NewPerson.MK上的Date_table中的NewDate等于NewDate.FK加入gj=Group From gj.DefaultIfEmpty()中的subperson,其中(NewPerson.Team=“A”)和also(subperson为Nothing或lse((subperson.Allocated“Vacation”)和(subperson.Allocated“Sick”))选择New With{NewPerson.MK,NewPerson.First_Name,NewPerson.Last_Name,NewPerson.Team
}
对于查询中的每个v
ListBox1.Items.Add($“{v.MK,-15}{v.First_Name,-15}{v.Last_Name,-15}{v.Team}”)
下一个
端接头
末级
类新人
公共属性MK作为字符串
公共属性名为字符串
公共属性姓氏为字符串
公共财产小组作为字符串
末级
类NewDate
公共属性FK作为字符串
公共属性日期作为字符串
作为字符串分配的公共属性
末级
其效果如下: