Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying a parameter in C#'s OrderBy methods

Tags:

c#

Good morning,

Let's imagine I have a list of Tuple elements, and a function taking a String and returning a Double, for example. How can I, from some other method, use the list's OrderBy method with that function calculated only on the first coordinate of each tuple? For example, return List.OrderBy(FunctionTakingString(Tuple'sFirstCoordinate)).First ?

Thank you very much.

like image 203
Miguel Avatar asked Mar 10 '26 19:03

Miguel


1 Answers

Just do:

return list.OrderBy(x => CustomFunction(x.Item1))
           .First();

OrderBy just needs to be provided with a delegate to compute a value from an element. Within the delegate you can do what you want, within reason.

like image 86
Jon Skeet Avatar answered Mar 12 '26 08:03

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!