Find the item with the lowest value of a property within a list

后端 未结 4 2017
你的背包
你的背包 2021-01-18 00:45

Let\'s say I have this class:

class Person {
   public int ID;
   public string Name;
}

And then I have a list of Person\'s.



        
4条回答
  •  悲哀的现实
    2021-01-18 01:19

    You can use MinBy method from More Linq library:

    var person = persons.MinBy(x => x.ID);
    

    If you can't use a third party library you can get the min ID first and then get the person that has the min ID:

    var minID = person.Min(x => x.ID);
    var person = persons.First(x => x.ID == minID);
    

提交回复
热议问题