在c#的list集合操作中,有时候需要查找到list集合中的最小值,此时可以使用list集合的扩展方法min方法,min方法有2种形式,一种是不带任何参数的形式,适用于一些值类型变量的list集合,另一种是带lambda表达式书写形式的,此方法可适用于获取list集合中某一个属性的最小值。
(1)不带任何参数的min方法形式举例,程序调用形式如下:
list<int> list1 = new list<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; var minvalue = list1.min();
运算结果为:minvalue =10。
(2)带lambda表达式书写形式的min方法举例
我们需要获取list<testmodel>集合对象testlist集合中对象属性index的最小值,首先看下testmodel的定义:
public class testmodel { public int index { set; get; } public string name { set; get; } }
获取testlist集合中的所有对象的index属性最小值可使用下列语句:
list<testmodel> testlist = new list<consoleapplication1.testmodel>(); var minvalue = testlist.min(t => t.index);
备注:原文转载自博主个人站it技术小趣屋,原文链接c#中list集合使用min()方法查找到最小值_it技术小趣屋。
黄山市民网:https://www.huangshanshimin.com/