我正在尝试将属性与linq的日期进行比较,我收到错误'Pages_ColumnDropdown_Default'含糊不清。我正在使用asp.net 4.0。这是代码
Private Class SomeData
Public Property El0 As Integer
Public Property El1 As String
Public Property El2 As String
Public Property El3 As String
Public Property eDate As DateTime
End Class
Dim oS As New List(Of SomeData)
Dim o As New SomeData
With o
.El0 = 1
.El1 = "Element 1 - Row 1"
.El2 = "Element 2 - Row 2"
.El3 = "Element 2 - Row 3"
.eDate = CDate("2/1/2014")
oS.Add(o)
End With
o = New SomeData
With o
.El0 = 2
.El1 = "Element 1 - Row 1"
.El2 = "Element 2 - Row 2"
.El3 = "Element 2 - Row 3"
.eDate = CDate("12/1/2013")
oS.Add(o)
End With
o = New SomeData
With o
.El0 = 3
.El1 = "Element 1 - Row 1"
.El2 = "Element 2 - Row 2"
.El3 = "Element 2 - Row 3"
.eDate = CDate("1/1/2014")
oS.Add(o)
End With
Dim mm As String = Date.Today.ToString
Dim mList = From w In oS
Where w.eDate = CDate("1/14/2014")
Select w
我也试过用mm。无论如何..似乎我之前已经完成了这么多次,但从未遇到过这个错误。我希望有人能指出我正确的方向
此位添加以回应建议
我已经改变了一些代码以反映建议的内容。通过这个
Dim cultureinfo As New System.Globalization.CultureInfo("en-us")
Dim mm As String = Now.ToShortDateString
Dim mList = From w In oS
Where w.eDate = DateTime.Parse("12/1/2014", cultureinfo)
Select w
linq语句运行并且没有错误。但是,如果我试着让它变得更有活力并且投入使用 mm为“12/1/2014”的日期,错误返回。所以改变后的代码无法恢复为
Dim cultureinfo As New System.Globalization.CultureInfo("en-us")
Dim mm As String = Now.ToShortDateString
Dim mList = From w In oS
Where w.eDate = DateTime.Parse(mm, cultureinfo)
Select w
当我看到DateTime.Parse(mm,cultureinfo)被渲染为什么时,这是结果
? DateTime.Parse(mm, cultureinfo)
#1/16/2014#
这是硬编码时的样子
? DateTime.Parse("1/16/2014", cultureinfo)
#1/16/2014#
这两个看起来和我相同..所以再一次......我错过了什么。希望有人能指教我。 谢谢 香农
答案 0 :(得分:0)
看起来日期的转换不了解文化因此不知道“12/1/2013”是否是1月12日(例如,如果文化是en-gb)或12月1日(例如,如果文化是我们的。)
您可以在解析日期字符串时显式设置文化:
Dim cultureinfo As New System.Globalization.CultureInfo("en-us")
... later in your code ...
.eDate = DateTime.Parse("12/1/2013",cultureinfo)