开发者

Specifying a parameter in C#'s OrderBy methods

开发者 https://www.devze.com 2023-01-22 04:56 出处:网络
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 wit

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(FunctionTa开发者_JAVA百科kingString(Tuple'sFirstCoordinate)).First ?

Thank you very much.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消