Reading multiple classes from single csv file using CsvHelper

前端 未结 1 892
名媛妹妹
名媛妹妹 2021-01-13 22:38

I\'ve been using Josh Close\' CsvHelper a bit recently to parse CSV files, I quite like the fluent api for class mapping.

I\'m trying to map a csv file which contain

相关标签:
1条回答
  • 2021-01-13 23:03

    There is a way to do this; you just have to do it manually.

    1. You manually read the csv file row by row
    2. Inspect the first column for the discriminator that will indicate that you need to map to a Class object.
    3. Inspect the second column for the class to map to.
    4. Map the entire row to that given class.

      public static void ReadMultiClassCsv()
      {
          var class1Data = new List<Class1>();
          var class2Data = new List<Class2>();
      
          using (StreamReader reader = File.OpenText(@"C:\filename.csv"))
          using (var csvReader = new CsvReader(reader))
          {
              //1.  You manually read the csv file row by row
              while (csvReader.Read())
              {
                  var discriminator = csvReader.GetField<string>(0);
      
                  //2.  Inspect the first column for the discriminator that will indicate that you need to map to a Class object.
                  if (discriminator == "D")
                  {
                      var classType = csvReader.GetField<string>(1);
      
                      //3.  Inspect the second column for the class to map to.
                      switch (classType)
                      {
                          //4.  Map the entire row to that given class.
                          case "Class1":
                              class1Data.Add(csvReader.GetRecord<Class1>());
                              break;
                          case "Class2":
                              class2Data.Add(csvReader.GetRecord<Class2>());
                              break;
                          default:
                              break;
                      }
                  }
              }
          }
      }
      
    0 讨论(0)
提交回复
热议问题