在特定点将字符串值添加到对象列表

时间:2016-08-31 07:52:05

标签: c# list

我有这个类用于存储从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

任何帮助都会很棒的人

2 个答案:

答案 0 :(得分:2)

试试这个:

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

答案 1 :(得分:1)

另一种处理方法:

webview