LINQ 加入使用日期不起作用
LINQ join using dates not working
我正在尝试基于两个单独的列表创建单个项目列表,但 ListA 中的日期 属性 与 ListB 中的不同日期 属性 具有相同的值。我从我的 FinalList 中的 ListB 中获得了正确的对象,但不是 ListA 中的对象。
考虑以下数据
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
,请执行简单的 Join
:
Dim FinalList = (From LA In ListA
Join LB In ListB On LA.InstallDate Equals LB.RemovalDate).ToList()
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
我正在尝试基于两个单独的列表创建单个项目列表,但 ListA 中的日期 属性 与 ListB 中的不同日期 属性 具有相同的值。我从我的 FinalList 中的 ListB 中获得了正确的对象,但不是 ListA 中的对象。
考虑以下数据
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
,请执行简单的 Join
:
Dim FinalList = (From LA In ListA
Join LB In ListB On LA.InstallDate Equals LB.RemovalDate).ToList()
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