查找最小数量并将属性设置为值

时间:2014-06-07 18:56:01

标签: vb.net linq

我有一个具有属性

的Item对象列表
- DateValue
- IsMin

如何查看列表并设置IsMin = true,其中DateValue是列表中的最小日期值?

2 个答案:

答案 0 :(得分:2)

这样的事情应该有效:

YourList.OrderBy(Function(item) item.DataValue).First.IsMin = true

OrderBy按DataValue排序

首先返回第一个元素

然后IsMin = true将IsMin设置为true ...

答案 1 :(得分:1)

像这样:

Class MyItem
  Public Property DateValue As DateTime
  Public Property IsMin As Boolean
End Class

Sub Main()
  Dim lst As New List(Of MyItem)
  lst.Add(New MyItem With {.DateValue = Today})
  lst.Add(New MyItem With {.DateValue = Today.AddDays(-1)})
  lst.Add(New MyItem With {.DateValue = Today.AddDays(1)})

  Dim v = From itm As MyItem In lst
          Where itm.DateValue = lst.Min(Function(x) x.DateValue)

  v(0).IsMin = True
End Sub