Reputation: 35785
Assume you have a list of edges with given length (as double). Now you want to find the edge with maximal length. Is there an easy way to do this in LINQ?
Of course, I can first compute the maximal value with Max and then do comparison, but firstly, this would be a two computations and secondly comparing doubles for equality is a bad thing.
Any suggestions?
Upvotes: 2
Views: 140
Reputation: 392833
Jon Skeet has MaxBy
in his MoreLINQ library: http://code.google.com/p/morelinq
Also, look at Observable.MaxBy
Alternatively look at
e.OrderByDescending(x => x.SomeProperty).First();
Upvotes: 6
Reputation: 2451
sehe beat me to the correct answer, so I'll use this to note a possibly flawed assumption:
...secondly comparing doubles for equality is a bad thing.
IIRC there shouldn't be any problem comparing a copied double for equality. After all it's just 8 bytes. The problem occurs in computation - or using two similar values from different sources.
That said, it's good to be scared of comparing floating point numbers :P
Upvotes: 0