Vb.net 使用日期的LINQ联接不起作用
我试图基于两个单独的列表创建一个项目列表,但是ListA中的日期属性与ListB中的不同日期属性的值相同。我从FinalList中的ListB中获得正确的对象,但不是ListA中的对象 考虑以下数据Vb.net 使用日期的LINQ联接不起作用,vb.net,linq,Vb.net,Linq,我试图基于两个单独的列表创建一个项目列表,但是ListA中的日期属性与ListB中的不同日期属性的值相同。我从FinalList中的ListB中获得正确的对象,但不是ListA中的对象 考虑以下数据 Public Class Foo Public Property InstallDate As Nullable(Of Date) 'getters/ setters End Property Public Property RemovalDate As
Public Class Foo
Public Property InstallDate As Nullable(Of Date)
'getters/ setters
End Property
Public Property RemovalDate As Nullable(Of Date)
'getters/ setters
End Property
End Class
Dim Foo1 As New Foo()
Foo1.InstallDate = 01/12/2014
Foo1.RemovalDate = Nothing
Dim Foo2 As New Foo()
Foo2.InstallDate = 02/12/2014
Foo2.RemovalDate = Nothing
Dim Foo3 As New Foo()
Foo3.InstallDate = 01/01/2001
Foo3.RemovalDate = 01/12/2014
Dim OriginalList As IList(Of Foo) = {
Foo1,
Foo2,
Foo3
}
还有我的密码
Dim ListA As IList(Of Foo) =
(From X In OriginalList
Where X.InstallDate IsNot Nothing And X.RemovalDate Is Nothing
Select X).ToList()
Dim ListB As IList(Of Foo) =
(From X In OriginalList
Where X.RemovalDate IsNot Nothing
Select X).ToList()
Dim FinalList As IList(Of Foo) =
(From LA In ListA
Group Join LB In ListB On ListA.InstallDate Equals ListB.RemovalDate Into Group _
From grp In Group.DefaultIfEmpty()
Select grp).ToList()
所以我希望FinalList包含对象Foo1和Foo3,但我只得到Foo3
知道我做错了什么吗?我已经更正了您的代码:
Public Class Foo
Public Property InstallDate As Nullable(Of Date)
Public Property RemovalDate As Nullable(Of Date)
End Class
Dim Foo1 As New Foo()
Foo1.InstallDate = #01/12/2014#
Foo1.RemovalDate = Nothing
Dim Foo2 As New Foo()
Foo2.InstallDate = #02/12/2014#
Foo2.RemovalDate = Nothing
Dim Foo3 As New Foo()
Foo3.InstallDate = #01/01/2001#
Foo3.RemovalDate = #1/12/2014#
Dim OriginalList As IList(Of Foo) = {
Foo1,
Foo2,
Foo3
}
Dim ListA = (From X In OriginalList
Where X.InstallDate IsNot Nothing AndAlso X.RemovalDate Is Nothing
Select X).ToList()
Dim ListB = (From X In OriginalList
Where X.RemovalDate IsNot Nothing
Select X).ToList()
因此,要获得Foo1和Foo3,请执行一个简单的连接:
FinalList现在包含单个项,因为有一个匹配项具有两个属性:
LA Foo1和LB Foo2
针对你的评论:
要使列表变平,只需使用:
Dim FinalList = From LA In ListA
Join LB In ListB On LA.InstallDate Equals LB.RemovalDate
From item in {LA, LB}
Select item
你在上什么课?安装日期是什么时候?什么是拆除日期?为什么要使用IListOf对象?一些输入/输出示例如何?否则,这是不可能回答的。RemovalDate是一个可以为Nothing的类型吗?@puropoix请注意,VB.Net也不使用Nothing来获取类型的默认值,如C的默认值。@sloth已编辑了Question我需要FinalList是一个单独的对象列表,以便随后进行迭代。
Dim FinalList = From LA In ListA
Join LB In ListB On LA.InstallDate Equals LB.RemovalDate
From item in {LA, LB}
Select item