C# Point3DCollection select 按最小或最大坐标点

C# Point3DCollection select point by min or max coordinate

我有数百个点的 Point3DCollection Point3D(2, 5, 8), Point3D(8, 6, 9), Point3D(5, 8, 12)... 我需要获得具有最低和最高 X 的坐标。我知道如何找到最低值(使用 LINQ Min),但我不知道如何找到最低值并获得它的 Y 和 Z。 你能帮帮我吗?

假设您的 Point3DCollectionPoint3D class 对象的列表。你的 Point3D class 看起来像这样。

public class Point3D
{
  public int X;
  public int Y;
  public int Z;

  public Point3D(int x, int y, int z)
  {
    X = x;
    Y = y;
    Z = z;
  }
}

您想要的 Linq 将是

最低 X

Point3D p = Point3DCollection.OrderBy(x => x.X).FirstOrDefault();

最高x

if(Point3DCollection.Count > 0)
   Point3D p = Point3DCollection.OrderBy(x => x.X).Last();

你最好先检查 Point3DCollection 是否为空。

here is working sample