Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 查找最小值并将属性设置为值_Vb.net_Linq - Fatal编程技术网

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)