本文介绍了在列表中找到属性值最低的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有这个课程:
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; }更多推荐
在列表中找到属性值最低的项目
发布评论