I'm working on huge amounts of data objects and I need to be able to convert them to text files based off of a csv header map. The header map is basically a list of fields that need to be printed out in that particular order e.g. if the header map is: ID, Name, Location etc, then those three fields of all objects need to be written to a csv file.
The problem is that the list of objects can be in millions and the header map can have hundreds of fieldnames in there - which will always be valid fieldnames in objects. Currently, the code I'm working with iterates over the entire list of objects and uses a switch statement over a map of all csv values to print out required fields. The code looks like this:
foreach (Object o in Objects)
{
foreach (String FieldName in CsvFieldsList)
{
switch (FieldName)
{
case "ID":
//Do something for this specific ID:
outString.Append(o.ID);
break;
....
....
}
}
}
AFAIK, C# will use a dictionary for string switch statements so those should be fast, however, this code is taking hours for some of our large datasets and I'm wondering if it's possible to improve this design.