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).
minby source code (it's pretty straightforward , has no dependencies on other files).
Comments
Post a Comment