在列表中查找具有最大值的项目 [英] Finding an item in a list with maximum value

查看:71
本文介绍了在列表中查找具有最大值的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是VB.Net的初学者.在我正在处理的代码中,有一个叫做Market的类,带有一个Demand(整数)属性.我列出了所有Market实例.在代码中的某个时刻,我需要在列表中找到需求值最高的Market实例.我如何有效地做到这一点(不循环遍历列表的所有成员,并将其需求与最低发现值进行比较). 谢谢您的帮助 美好的一天!

I m a beginner in VB.Net. In the code I m working on, there is a class called Market with an attribute of Demand (integer). I have made a list of all instances of Market. At some point in the code, I need to find the Market instance in the list with highest demand value. How can I do that efficiently (without looping over all members of the list and comparing their demand with the lowest found value). Thank you for your help Good day!

推荐答案

您可以使用OrderByDescending方法按Demand属性对List进行排序,然后选择第一项.

You can use the OrderByDescending method to sort the List by the Demand property and then select the first item.

    Dim markets As New List(Of Market)
    markets.Add(New Market With {.Demand = 10})
    markets.Add(New Market With {.Demand = 30})
    markets.Add(New Market With {.Demand = 20})
    Dim topMarket As Market = markets.OrderByDescending(Function(m) m.Demand).FirstOrDefault

这篇关于在列表中查找具有最大值的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆