2016-08-31 60 views
2

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

我想要做的是将一个StdDevX + "," + StdDevY的字符串添加到每个sorted.csv,并使用不同的名称。正如你可以在我的代码底部看到的,我正在将sorted.csv写入一个excel文件(作为逗号分隔值)。这里是我的预期输出来说明我need

任何帮助将是伟大的球员

回答

2

试试这个:

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

嗯,我得到的错误'连接点不包含定义为“ForEach”?也许我正在做一些愚蠢的事情。 –

+0

对,我的不好。应该是:'sorted.Last(n => n.point == name).csv + = StdDevX“,”+ StdDevY;' – owczarek

1

处理这个另一种方式:

// Equal to your distinct, but returns the point objects instead. 
List<cPoint> lstPoints = sorted 
    .GroupBy(x => x.point) 
    .Select(g => g.First()) 
    .ToList(); 

// Loop through all points with a distinct .point property 
foreach (cPoint point in lstPoints) 
{ 
    // Get all Values Where the name is equal to the point.name in List; Select all xPoint Values 
    double[] x_array = sorted.Where(n => n.point == 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 point.name in List; Select all yPoint Values 
    double[] y_array = sorted.Where(n => n.point == point.name).Select(x => x.yPoint).ToArray(); 
    string StdDevY = Convert.ToString(Statistics.StandardDeviation(y_array)); 

    // Edit the existing item of the list 
    point.csv += "," + StdDevX + "," + StdDevY; 
} 

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

我最初尝试过,但它给了我一些错误'只有赋值,调用, dec和表达式可以是一个语句'并且'字符串不包含“csv”'的定义。除非我错误地应用了陈述 –

+0

@ChristopherDyer我看到,您正在使用字符串列表。错过了我第一次阅读。更新了我的答案。 – DeveloperExceptionError

+0

啊,我明白我现在做错了什么。您的解决方案可能是最优雅的,但接受的答案是提供的代码的快速解决方案。这是我无法接受的一种耻辱。 –

相关问题