我有用于存储从XML文件读取的数据的此类:

public class cPoint
{
    public string point;
    public string time;
    public double xPoint;
    public double yPoint;
    public string csv;
}


然后,我还有另一个类来搜索XML文件并将数据存储在全局的List<cPoint> sorted = new List<cPoint>();中:

...
if (measurementType == "Body")
{
    cPoint Point = new cPoint();
    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));
    csvString = endTime + "," + location + "," + xOffset + "," + yOffset;
    Point.csv = csvString;
    sorted.Add(Point);
}


最后,我在主体中使用以下代码对sorted中的不同名称进行排序,并计算关联的xPointyPoint的标准偏差:

List<string> PointNames = sorted.Select(x => x.point).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 = sorted.Where(n => n.point == name).Select(x => x.xPoint).ToArray();
    string StdDevX = Convert.ToString(Statistics.StandardDeviation(x_array));

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

    //Something along these lines:
    //sorted.csv += StdDevX "," + StdDevY;
}

List<string> CSV = sorted.Select(x => x.csv).ToList();
WriteToFile(Title);
WriteToFile(CSV);


我想做的是在每个sorted.csv中添加一个具有唯一名称的StdDevX + "," + StdDevY字符串。如您在代码底部所看到的,我正在将sorted.csv写入一个Excel文件(以逗号分隔的值)。这是我的预期输出,以说明我的need

任何帮助都是很棒的人

最佳答案

尝试这个:

sorted.Last(n => n.point == name).csv += StdDevX "," + StdDevY;

10-07 19:19
查看更多