用户在.net中输入数据类型为date

时间:2012-06-06 15:46:29

标签: .net vb.net datetime date sql-order-by

我尝试使用“Order by”来订购最早在VB.Net中开始的日期列表。

变量和方法是:

Dim SetupStart As DateTime 
Dim SetupTimeList As New List(Of Date)

'方法p.ReadFieldDateTime用于连接SQL数据库并读取字段设置时间

SetupStart = p.ReadFieldDateTime("Orders", "Setup Time", record)
SetupTimeList.Add(SetupStart)

Dim SetupTimeListeOrdered = from SetupTime In SetupTimeList
Order By SetupTime

我希望这段代码片段有意义,如果它没有任何意义我会很高兴如果有人能告诉我如何在最早的日期之前订购此列表。

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

您希望OrderBy(...)使用IEnumerable扩展程序,如下所示:

Dim SetupTimeListeOrdered = SetupTimeList.OrderBy(Function(d) d)

我对VB.NET语法不太确定,抱歉。

在C#中:`List SetupTimeListOrdered = SetupTimelist.OrderBy(d => d);

答案 1 :(得分:0)

通过测试以下代码,它运行良好,我不确定您使用的方法来获取Date值:

Sub Main(args As String())         Dim dtList As List(Of Date)= CreateListofDateTime()

    Dim NonOrderedList = From t In dtList

    Console.WriteLine("Non Ordered List")
    For Each item As DateTime In NonOrderedList
        Console.WriteLine(item.ToString())
    Next

    Dim OrderedList = From t In dtList Order By t
    Console.WriteLine("Ordered List")
    For Each item As DateTime In OrderedList
        Console.WriteLine(item.ToString())
    Next
End Sub

Function CreateListofDateTime() As List(Of DateTime)
    Dim dtValue As New DateTime()
    Dim ldtListTosendBack As New List(Of Date)()
    dtValue = DateTime.Now
    ldtListTosendBack.Add(dtValue)

    dtValue = DateTime.Now.AddDays(5)
    ldtListTosendBack.Add(dtValue)
    dtValue = DateTime.Now.AddDays(-5)
    ldtListTosendBack.Add(dtValue)
    dtValue = DateTime.Now.AddDays(-9)
    ldtListTosendBack.Add(dtValue)
    dtValue = DateTime.Now.AddDays(-10)
    ldtListTosendBack.Add(dtValue)
    Return ldtListTosendBack
End Function