title: How to map columns from CSV to the class properties in C#?
body: <p><strong>TO DO</strong></p>
  
  <p>To create a console application, which can read a CSV file and auto determine to which of the three CSV files(varies in column data) it belongs to.</p>
  
  <p>I have created a console application, which is currently reading all the csv files from the directory using SmartXLS library. </p>
  
  <p>How can I read in the data for each file into a list and have a simple mechanism for mapping each "column" of data with the associated class property (i.e. column name) I have defined in my class?</p>
  
  <p><strong>File structure 1:</strong></p>
  
  <p>Usage Date,Product Name,User Id,Tokens Used</p>
  
  <ol>
  <li>20160428,Samsung,dorman@stackoverflow.com</li>
  </ol>
  
  <p><strong>File structure 2:</strong></p>
  
  <p>Reason, Month, Adjustment Date, Transaction ID, Tokens Adjusted, Product Name, Comment Added</p>
  
  <ol>
  <li>Off network, 15-Oct, 31-Oct-15,73820274918-230934049372,045, Xbox, Bridge Design for InfraWorks 360.</li>
  </ol>
  
  <p><strong>File structure 3:</strong>           </p>
  
  <p>Usage Date, Product Name, Product Version, User Id, Machine Name, Server Name, Tokens Used, Hours Used</p>
  
  <ol>
  <li>20161027, Gamepro, 2016, 11597, BC318010, LA847012, 6, 0.1</li>
  </ol>
  
  <p>Kindly, help me in re-writing the function.</p>
  
  <p>Class:</p>
  
  <pre><code>public class Token
       {
              public DateTime Usagedate { get; set; }
              public string Product_name { get; set; }
              public string Product_Version { get; set; }
              public string Userid { get; set; }
              public string User_name { get; set; }
              public string Machine_name { get; set; }
              public string Server_name { get; set; }
              public string Tokens_used { get; set; }
              public string Use_count { get; set; }
  
          }
  </code></pre>
  
  <p>App:</p>
  
  <pre><code>namespace ConsoleApp
  {
      class token
      {
          public List&lt;Token&gt; Tokens { get; set; }
          public void TokenData()
          {
              Console.WriteLine("Begin Harvesting");
  
              CategorizeData();//function
          }
  
   public void CategorizeData()
          {
              int count = 0;
  
              Tokens = new List&lt;Token&gt;();
  
              var files = Directory.EnumerateFiles(@"C:\Users\Projects", "*.csv");
  
              foreach (string file in files)
              {
  
                  SmartXLS.WorkBook WB = new WorkBook();
                  WB.readCSV(file);
  
                  DataTable dt = WB.ExportDataTable();
  
                  string dtSTR;
                  DataRow dr;
                  DataColumn dc;
                  for (int i = 1; i &lt; dt.Rows.Count; i++)
                  {
  
                      dr = dt.Rows[i];
  
                     try
                     {
                         // Map Columns with the class properties
                         // read them to the list
                       dtSTR = dr[0].ToString();
                      if (string.IsNullOrEmpty(dtSTR)) continue;
  
                      var tkn = new Token();
  
  
                      tkn.Usagedate = ParseDateTime(dtSTR);
                      tkn.Product_name = dr[1].ToString();
                      tkn.Userid = dr[2].ToString();
                      tkn.Tokens_used = dr[3].ToString();
                      tkn.Reason = dr[4].ToString();
  
  
                      Tokens.Add(tkn); 
                      count++;    
  
  
                      }
                      catch (Exception ex)
                      { }
  
  
                  }
  
              }
          }
      } 
  
  }
  </code></pre>