如何找到最早的时间值?

时间:2013-10-25 22:52:38

标签: vb.net

在我的项目中,在某些时候我倾向于加载我的DataSet中的所有行,并按照“Time”项的顺序将它们添加到我的FlowLayoutPanel中。这就是我想要做的,但是如果他们的“时间”值是最老的,我需要首先添加控件:

For i=1 to DataSet.Tables("myTable").Rows.Count
 Dim Row as Datarow = DataSet.Tables("myTable").Select("ID = " & i)
 Dim Time as Date = Row.Item("Time")
 Dim NewLabel as Label
 NewLabel.text = Time.ToString()
 FlowLayoutPanel.Controls.Add(newLabel)
Next

我该怎么做?

1 个答案:

答案 0 :(得分:1)

您可以使用Linq-To-DataSet

Dim timeOrderedRows = From row in DataSet.Tables("myTable")
                      Order By row.Field(Of Date)("Time")
For Each row As DataRow In timeOrderedRows
    Dim Time as Date = row.Field(Of Date)("Time")
    Dim Label as New Label
    Label.text = Time.ToString()
    FlowLayoutPanel.Controls.Add(Label)
Next
  

如何反向(从最新到最旧)

您只需添加Descending

....
Order By row.Field(Of Date)("Time") Descending