-1

Trying to manipulate the data in the following datatable to allow me to calculate averages.

I want to add all the data in PktsRecieved(XAxis) and PktsRecieved(YAxis) and calculate an average

Should I use a Linq query or what is the best way of doing this

Thanks

List<string[]> rows = File.ReadAllLines(@"E:\\Final Year\\Uni\\NoCAnalysisTool2\\rx.csv")
   .Select(x => x.Split(','))
   .ToList();

DataTable dt = new DataTable();

dt.Columns.Add("Source|Destination");
dt.Columns.Add("PktsRecieved(XAxis)");
dt.Columns.Add(" ");
dt.Columns.Add("Source|Destination ");
dt.Columns.Add("PktsRecieved(YAxis)");
dt.Columns.Add("");
dt.Columns.Add("");
dt.Columns.Add("");


rows.ForEach(x => { dt.Rows.Add(x); });
dataGridView2.DataSource = dt;

1 Answer 1

0

Well, if I'm reading it right, your biggest problem might be that the data is in strings. Off the top of my head, I'd suggest something like this:

double averageXAxis = rows.Average( (r) => double.Parse(r[1]) );

(forgive me if the syntax isn't completely correct - I'm typing it freehand without VS at the moment.)

But like I said, you might have a problem in that your data is technically in a string format coming from a .CSV - which means there could technically be anything in it - numbers, letters, weird non-ascii chars, etc. You might have to add additional LINQ statements to narrow it down to only the rows that have a parse-able value in the column. You probably don't want to code it in a way that a single blank/rogue value causes the whole code to Exception.

Edit: If you need to operate on the DataTable and don't have access to that rows object? You can use the AsEnumerable() function to get a row enumeration on the DataTable and do the same sort of LINQ trick with .Average(...)

Sign up to request clarification or add additional context in comments.

Comments

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.