Vb.net 查找最小值并将属性设置为值
我有一个具有属性的项对象列表Vb.net 查找最小值并将属性设置为值,vb.net,linq,Vb.net,Linq,我有一个具有属性的项对象列表 - DateValue - IsMin 如果DateValue是列表中的最小日期值,如何查看列表并设置IsMin=true?如下: 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
- IsMin
如果DateValue是列表中的最小日期值,如何查看列表并设置IsMin=true?如下:
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
像这样的方法应该会奏效:
YourList.OrderBy(函数(项)项.DataValue.First.IsMin=true
OrderBy按数据值排序
First返回第一个元素
然后IsMin=true将IsMin设置为true…类似于:
YourList.OrderBy(Function(item)item.DataValue)。First.IsMin=true
@DanielCook:实际上这看起来是个不错的答案。你应该把它贴在下面。有没有可能有多个对象具有相同的MinumDateValue?是的,我考虑过提供这个建议。很好的设置代码,因为OP没有提供任何设置。;-)+1.我更喜欢你的选项——它将最小值和索引操作结合在一起。所以你的第一次
=我的分钟
+v(0)
。