2016-08-31 61 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

啊,我明白我現在做錯了什麼。您的解決方案可能是最優雅的,但接受的答案是提供的代碼的快速解決方案。這是我無法接受的一種恥辱。 –

相關問題