2

I have this class for storing data read from an XML file:

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

I then have another class to scour the XML file and store data in List<cPoint> sorted = new List<cPoint>(); which is global:

...
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);                 
}

and finally I use this code in my main to sort through the distinct names in sorted and calculate the standard deviation of the associated xPoint and yPoint:

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);

What I would like to do is add a string of StdDevX + "," + StdDevY to each sorted.csv with a distinct name. As you can see at the bottom of my code, I'm writing sorted.csv to a an excel file (as a comma separated value). Here is my expected output to illustrate what I need.

Any help would be great guys

2 Answers 2

2

Try this:

sorted.Last(n => n.point == name).csv += StdDevX "," + StdDevY;
Sign up to request clarification or add additional context in comments.

2 Comments

Hmm, I'm getting the error cPoint does not contain a definition for "ForEach"? Perhaps I'm doing something stupid..
Right, my bad. Should be: sorted.Last(n => n.point == name).csv += StdDevX "," + StdDevY;
1

Another way of handling this:

// 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);

4 Comments

I tried that initially but it gave me a few errors only assignment, call, inc, dec and expressions can be a statement and string does not contain a definition for "csv". Unless I was applying the statement incorrectly
@ChristopherDyer I see, you are using a string list. Missed that on my first read. Updated my answer.
Ah, I see what I did wrong now. Your solution is probably the most elegant but the accepted answer is a quick solution for the code provided. It's a shame I can't accept both.
@ChristopherDyer I even thought about deleting it after I had seen the existing answer and your commend. Good thing I did update it. Glad I could help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.