Your problem right now is you have the data (even though getting it is ugly), but it is all in string
s. No matter what you do, you will have to convert to a decimal
or other numeric format to do math on it.
I would recommend using a library like FileHelpers to read your CSV data into an "employee" class first. This will give you strongly typed objects. See the 'Quick Start Delimited' entry on the left side. your class would look something like this:
[DelimitedRecord(",")]
public class Employee {
// fields in same order as in the file
public string EmployeeId { get; set; }
public string EmployeeName { get; set; }
public int MyNumber { get; set; }
}
Suggestions for current code:
- What is
rawFile
?? you get the lines using ReadAllLines() - Follow .NET naming guidelines.
var file = ...
notvar File =
- Don't use the same name as common .Net classes (e.g. File). Call it
fileLines
, etc - Don't read the file twice to get the # of lines. use
new string[fileLines.Count, 4]
- You can use LINQ and calls to split easier if you don't use a
[,]
multi-dimensional array. - To convert between string and int, you will need
int.Parse
orint.TryParse
- Add error checking to make sure your lines are the correct length, etc
Some sample code:
var data = fileLines.Select(line => line.Split(','))
.Where(arr => arr.Length == 4) // should have 4 fields
.ToArray();
var xxxx = data[0][1]; // should be "EmployeeNameHere"
// this assumes your data is ALL valid
var numbers = data.Select(x => int.Parse(x[2])).ToList();
var sum = numbers.Sum();
// there is no "pretty" way to do TryParse
var numbers = new List<int>();
foreach(var arr in data) {
int temp = 0;
if (int.TryParse(arr[2], out temp)) {
numbers.Add(temp);
}
}