MassUploadUser
Based on the .NET Naming Guidelines properties should be named using PascalCase
casing.
The loop
In its current form you do for each column in each row a switch
to get the column index and value. By extracting this to a separate method like so
private Dictionary<string, int> ReadColumnIndexes(string[] headers)
{
return headers.Select((v, i) => new { Key = v, Value = i })
.ToDictionary(o => o.Key, o => o.Value);
}
You can then, outside of the while
loop, assign the desired column index to variables like so
var columnDictionary = ReadColumnIndexes(headers);
var firstNameColumn = columnDictionary["First Name"];
var lastNameColumn = columnDictionary["Last Name"];
.....
var passwordColumn = columnDictionary["Password"];
now your loop could look like so
while (!parser.EndOfData)
{
string[] fields = parser.ReadFields();
var massUploadUser = new MassUploadUser();
massUploadUser.firstname = fields[firstNameColumn];
massUploadUser.lastname = fields[lastNameColumn];
.....
string password = fields[passwordColumn];
// After the for loop, I have some additional logic
// to serialize the newly-created object to JSON
// and then POST it to a RESTful API
DoPostToWebService(massUploadUser, password);
// At this point, we created the new user on the server, so no need
// to keep the current instance of massUploadUser around
}
Heslacher
- 50.9k
- 5
- 83
- 177
default