0

Visual Studio with 2019 and C#

I have a flat file with data in this format:

CustomerName<tab>CustomerAddress<tab>CustomerEmail<tab>CustomerPhone etc....
Tom<tab>123 Chestnut Street, NY, NY 10001<tab>Tom@gmail.com<tab>212-555-1212
Dick<tab>234 Oak Street, LA, CA 91210<tab>Dick@gmail.com<tab>747-555-1212
Harry<tab>789 Maple Drive, Reno, NV 89508<tab>Harry@yahoo.com<tab>775-555-1212
etc...

The columns can vary from about 5 to 15 columns, depending on the customer file given to us.

Our users want to be able to preview a sample of the data, anywhere from 1 to 50 sample rows of data. However they insist on seeing the data pivoted on the page:

Column1                  Column2             Column3              etc...
Tom                      Dick                Harry
123 Chestnut Street...   234 Oak Street...   789 Maple Drive...
Tom@gmail.com            Dick@gmail.com      Harry@yahoo.com
212-555-1212             747-555-1212        775-555-1212
etc...

I have this special customer class for this purpose:

public class Customer {
public string Column1 { get; set; }
public string Column2 { get; set; }
public string Column3 { get; set; } 
//etc.... to 50 columns
}

But I am having difficulty coming up with an elegant way to code this. If I can get the data into a System.Data.DataTable in that pivoted form, I can easily get it to an html table. Any thoughts on how to cycle through the rows of data to get it into the class? Not sure what are the best intermediate classes to use. I don't want to hard code something like this:

...
for (int i = 0; i < columnCount; i++) {
  Customer c = new Customer();
  c.Column1 = row1[i].ToString();  
  c.Column2 = row2[i].ToString();  
  c.Column3 = row3[i].ToString();  
//etc...
}
BarnumBailey
  • 391
  • 1
  • 4
  • 13

1 Answers1

0

My approach would be a little different, and would constrast from the data structure that you created. I would instead deserialize the flat file rows into an array objects containing an array of strings. This way I don't have to hard-code anything and my "row count" and my object data properties length is dynamic.

// this is for a row's data
public class RowObject
{
    public List<string> RowObjectProperty;
    public RowObject()
    {
        RowObjectProperty = new List<string>();
    }
}


// ===================================================================
// ... somewhere in a class which deserializes the flat file

List<RowObject> rows = new List<RowObject>();

// ...read data into the rows....

// ...print out the data for each row as your columns
foreach (RowObject customer in rows)
{
    foreach (string customerProperty in customer)
    {
        // here is each customer property (name, email, phone, etc)
        // ... create your column for this customer here     ​
   ​}
}
// ===================================================================

It's a bit rudimentary, but I feel should get you going in a better direction. Now, if you already know that the data structure (customer data) will have specific and consistent data fields/types, then I would parse the flat file like suggested here: https://stackoverflow.com/a/18479420/1188197

EspressoBeans
  • 1,747
  • 12
  • 22
  • 1
    I am reworking the logic according to your suggestion, I think the array approach is best as I don't know the data structure. Thanks. :) – BarnumBailey Oct 06 '21 at 19:13