在列表中找到属性值最低的项目 [英] Find the item with the lowest value of a property within a list
本文介绍了在列表中找到属性值最低的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有这个课程:
class Person {
public int ID;
public string Name;
}
然后我有一个人的清单.
And then I have a list of Person's.
List<Person> persons = new List<Person>();
其中充满了很多随机的人.如何查询列表以获取ID最低的人?列表中的对象是随机顺序的,因此ID最低的人可能不是第一个元素.我可以在不先对列表进行排序的情况下实现此目标吗?
Which is filled with alot of random persons. How do I query the list to get the person with the lowest ID? The objects in the list is in a random order so the person with the lowest ID might not be the very first element. Can I achieve this without sorting the list first?
推荐答案
这是不对列表进行排序,而只是对列表进行一次迭代.
this is without sorting the list and just iterates the list once.
Person minIdPerson = persons[0];
foreach (var person in persons)
{
if (person.ID < minIdPerson.ID)
minIdPerson = person;
}
这篇关于在列表中找到属性值最低的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文