c# - How to use linq to find the minimum -


this question has answer here:

i have class { public float score; ... } , ienumerable<a> items , find a has minimal score.

using items.min(x => x.score) gives minimal score , not instance minimal score.

how can instance iterating once through data?

edit: long there 3 main solutions:

  • writing extension method (proposed svish). pros: easy use , evaluates score once per item. cons: needs extension method. (i choosed solution application.)

  • using aggregate (proposed daniel renshaw). pros: uses built-in linq method. cons: obfuscated untrained eye , calls evaluator more once.

  • implementing icomparable (proposed cyberzed). pros: can use linq.min directly. cons: fixed 1 comparer - can not freely choose comparer when performing minimum computation.

have @ minby extension method in morelinq (created jon skeet, principally maintained atif aziz).


Comments

Popular posts from this blog

Delphi Wmi Query on a Remote Machine -