How to use linq to find the minimum

This question already has an answer here:

  • How to use LINQ to select object with minimum or maximum property value 12 answers

  • Have a look at the MinBy extension method in MoreLINQ (created by Jon Skeet, now principally maintained by Atif Aziz).

  • MinBy documentation

  • MinBy source code (it's pretty straightforward and has no dependencies on other files).


  • Use Aggregate:

    items.Aggregate((c, d) => c.Score < d.Score ? c : d)
    

    As suggested, exact same line with more friendly names:

    items.Aggregate((minItem, nextItem) => minItem.Score < nextItem.Score ? minItem : nextItem)
    

    尝试items.OrderBy(s => s.Score).FirstOrDefault();

    链接地址: http://www.djcxy.com/p/51348.html

    上一篇: 使用LINQ更新集合中的所有对象

    下一篇: 如何使用linq来查找最小值