查找列表中的更改然后将特定值存储到数组 C#

Finding changes in a list then storing specific values into an array C#

我有一个 class:

 namespace XMLParserAverage11
 {
     public class cPoint
     {
        public string point;
        public string time;
        public double xPoint;
        public double yPoint;
        public double StdDevX;
        public double StdDevY;
        public string csv;

       public cPoint(string n, double x, double y)
       {
           this.point = n;
           xPoint = x;
           yPoint = y;
       }

 }
}

这个用来存放读取XML文件得到的信息,然后放入

List<cPoint> Sorted

我想做的是将 "xPoint" 存储到一个数组中,将 "yPoint" 存储到另一个数组中,用于所有 "point" that has the same value。这样我就可以最终使用 Statistics.PopulationStandardDeviation()from Mathnet.Numerics NuGet 包,它需要一个双数组并计算 xPoint 和 yPoint 的总标准差 "point" 在 List Sorted.

if (measurementType == "Body")
{
    double a = Convert.ToDouble(xOffset);
    double b = Convert.ToDouble(yOffset);
    cPoint Point = new cPoint(location, a, b);
    Point.time = endTime;
    //  Point.point = location;
    //   Point.xPoint = Convert.ToDouble(xOffset);
    //    Point.yPoint = Convert.ToDouble(yOffset);
    sorted.Sort((x, y) => x.point.CompareTo(y.point));
    //   double[] balanceX = {Point.xPoint};
    //  double[] balanceY = {Point.yPoint};
    if (sixSig == true)
    {
        List<cPoint> pointList = new List<cPoint>();
        // Select all the distinct names
        List<string> pointNames = location.Select(x => xOffset).Distinct().ToList();

        foreach (var name in pointNames)
        {

            // Get all Values Where the name is equal to the name in List; Select all xPoint Values
            double[] x_array = pointList.Where(n => n.point == name).Select(x => x.xPoint).ToArray();
            Point.StdDevX = Statistics.StandardDeviation(x_array);

            // Get all Values Where the name is equal to the name in List; Select all yPoint Values
            double[] y_array = pointList.Where(n => n.point == name).Select(x => x.yPoint).ToArray();
            Point.StdDevY = Statistics.StandardDeviation(y_array);
        }

        csvString = Point.time + "," + Point.point + "," + Point.xPoint + "," + Point.yPoint + "," + Point.StdDevX + "," + Point.StdDevY;
        Point.csv = csvString;
        sorted.Add(Point);

    }
    else
    {
        csvString = endTime + "," + location + "," + xOffset + "," + yOffset;
        Point.csv = csvString;
        sorted.Add(Point);
    }
}

我的输出最终会类似于 this
显然我是初学者,所以任何帮助将不胜感激。

编辑:

ok 这是一个小型控制台应用程序,它说明了我认为您正在寻找的过程。为此,我删减(并重命名)了您的 class cPoint

public class C_Point
{
    public string point;
    public double xPoint;
    public double yPoint;

    public C_Point(string n, double x, double y)
    {
        this.point = n;
        xPoint = x;
        yPoint = y;
    }
}

class Program
{


    static void Main(string[] args)
    {
        List<C_Point> pointList = new List<C_Point>();

        pointList.Add(new C_Point("P1", 1, 11));
        pointList.Add(new C_Point("P1", 2, 22));
        pointList.Add(new C_Point("P1", 3, 33));
        pointList.Add(new C_Point("P10", 101, 111));
        pointList.Add(new C_Point("P10", 201, 211));
        pointList.Add(new C_Point("P10", 301, 311));

        // Select all the distinct names
        List<string> pointNames = pointList.Select(x => x.point).Distinct().ToList();

        foreach (var name in pointNames)
        {
            Console.WriteLine("Name: {0}", name);

            // Get all Values Where the name is equal to the name in List; Select all xPoint Values
            double[] x_array = pointList.Where(n => n.point == name).Select(x => x.xPoint).ToArray();

            Console.WriteLine(String.Join(" ", x_array));

            // Get all Values Where the name is equal to the name in List; Select all yPoint Values
            double[] y_array = pointList.Where(n => n.point == name).Select(x => x.yPoint).ToArray();

            Console.WriteLine(String.Join(" ", y_array));
        }
        Console.ReadKey();
    }
}

您可以将其复制粘贴到控制台项目中,然后让它 运行。 它将显示带有点名称的数组值,在您的示例中为 Location。 我是否更接近您的问题?

编辑 2:

当您尝试获取点的所有不同名称时,您需要从所有点所在的 List 中提取它们。

在这里,您尝试 select 来自 string 的内容。这行不通。

// Select all the distinct names
List<string> pointNames = location.Select(x => xOffset).Distinct().ToList();

改为使用 sorted 列表。我想你把它们都放在里面了,不是吗? Select(x=>x.point) 表示:x 是列表中任何元素(在您的类型 cPoint 的情况下)的占位符。在 => 之后,您指定应 select 编辑哪些 属性 元素!在这种情况下 x.point。结果将是所有元素的 属性 的所有值的列表。 Distinct 调用删除了多个条目。

这:(x => xOffset) 不起作用,因为它不是一个有效的表达式,并且因为您没有 属性 Offset 可以 select 在您的 classcPoint

这也行不通:

double[] x_array = pointList.Where(n => n.point == name).Select(x => x.xPoint).ToArray();

因为你的pointList是空的!您需要再次使用 List 和您所有的积分!

希望这对您有进一步的帮助。如果没有请写信给我。

有关更多信息,请阅读有关 Lambda expressions

的更多信息