获取点列表中的最大值

NoB*_*Man 0 .net c#

我有一个点列表,每个点定义为"XYPoint"类型的对象,具有X和Y成员.如何在点列表中找到具有最大X或最大Y值的点,而不是循环(Linq?)?

public class XYPoint
{
    int X;
    int Y;
}
List<XYPoint> lsRawPoints;
Run Code Online (Sandbox Code Playgroud)

DLe*_*Leh 6

最大X位置:

lsRawPoints.Max(point => point.X)
Run Code Online (Sandbox Code Playgroud)

最大Y位置

lsRawPoints.Max(point => point.Y)
Run Code Online (Sandbox Code Playgroud)

最大组件总和

lsRawPoints.Max(point => point.X + point.Y)
Run Code Online (Sandbox Code Playgroud)

等等.

  • +1.另一种有时必要的方法是`lsRawPoints.OrderBy(p => pX).Last()`. (3认同)