Get Max value from List

后端 未结 8 411
猫巷女王i
猫巷女王i 2021-02-02 05:08

I have List List, my type contains Age and RandomID

Now I want to find the maximum age from this list.

What

8条回答
  •  礼貌的吻别
    2021-02-02 05:29

    Assuming you have access to LINQ, and Age is an int (you may also try var maxAge - it is more likely to compile):

    int maxAge = myTypes.Max(t => t.Age);
    

    If you also need the RandomID (or the whole object), a quick solution is to use MaxBy from MoreLinq

    MyType oldest = myTypes.MaxBy(t => t.Age);
    

提交回复
热议问题