0

I have a .csv file that comes in this format. I'm using the FileHelpers library but I need some direction on parsing the file into a single record so the collection can be inserted into a database. At this point I believe the "Total" line can be ignored, but still waiting on confirmation.

 94506 - DANVILLE CA,,,,,,,,,
 Route,SFDU ,MFDU ,Total ,Names ,Income ,Home ,Age ,PHWC ,Sat 
 C055, 879, 0, 359, 442, $100, $100, 60, 21%, S
 Total, 0, 0, 0, 0,,,,,
,,,,,,,,,
 94518 - CONCORD CA,,,,,,,,,
 Route,SFDU ,MFDU ,Total ,Names ,Income ,Home ,Age ,PHWC ,Sat 
 C086, 578, 33, 785, 237, $100, $100, 49, 22%, S
 C087, 478, 37, 733, 337, $100, $100, 49, 22%, S 
 C088, 578, 36, 798, 437, $100, $100, 49, 22%, S
 Total, 0, 0, 0, 0,,,,,
Tim
  • 1,249
  • 5
  • 28
  • 54

1 Answers1

2

There is an easy example here

Basically, you need to define two classes, one for your master:

[DelimitedRecord("-")]
public class Master
{
   public string Zip;
   public string City;
}

And another for your detail

[DelimitedRecord(",")]
public class Orders
{
   public string Route;
   public int SFDU;
   public int MFDU;
   public int Total;
   public int Names;
   public string Income;
   // etc ...
}

Then you need a selector to determine which is which

RecordAction ExampleSelector(string record)
{
   if (record[0] == 'C') // if the line starts with a C
      return RecordAction.Detail;
   else if Char.IsNumber(record[0]) // if the line starts with a number
      return RecordAction.Master;
   else // skip anything else, e.g., Route, Total, etc.
      return Record.Action.Skip;
}
shamp00
  • 11,106
  • 4
  • 38
  • 81
  • I saw that example earlier but my concern was that it wouldn't work due to each detail section contains the column headers. I guess I could add the [IgnoreFirst(1)] on the detail entity and try it though. – Tim Aug 12 '14 at 16:37
  • In my sample code I return RecordAction.Skip for those lines. – shamp00 Aug 12 '14 at 21:42
  • I used skip for those as well. I made some additional modifications to the ExampleSelector but I just wrapped up the testing and it all works nicely. Thanks for the pointers. – Tim Aug 12 '14 at 21:51