0

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

变量和方法是:

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

'方法 p.ReadFieldDateTime 用于连接 SQL 数据库并读取字段 Setup Time

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

Dim SetupTimeListeOrdered = from SetupTime In SetupTimeList
Order By SetupTime

我希望这个代码片段有意义,如果没有任何意义,如果有人能告诉我如何按最早日期升序排列这个列表,我会很高兴。

谢谢你的帮助!

4

2 回答 2

0

您想像这样使用OrderBy(...)扩展名IEnumerable

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

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

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

于 2012-06-06T15:51:55.120 回答
0

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

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
于 2012-06-06T16:24:11.300 回答