How do I delete certain column from .csv file

后端 未结 1 936
逝去的感伤
逝去的感伤 2021-01-17 00:25

I am downloading the list of data but some i would like to ignore some columns, is there any way i can remove them, this is how my database look:

    ID  Nam         


        
相关标签:
1条回答
  • 2021-01-17 00:57

    I won't go into detail on how you get your data. So lets just assume you get a csv file.

    //class to strongly type our results
    public class csvClass
    {
       public csvClass(string name; string mu)
       {
         this.Name = name; this.MobileUsage = mu;
       }
       public string Name { get; set; }
       public string MobileUsage { get; set; }
    }
    
    //just load your csv from wherever you need
    var csvData = from row in File.ReadLines(@"Path/to/file.csv")
                  // data is still in one line. Split by delimiter
                  let column = row.Split(';')
                  //strongly type result
                  select new csvClass
                  {
                      //Ingore column 2 and 4
                      //Take first column
                      Name = column[0],
                      //Take third column
                      MobileUsage = column[2]
                  };
    

    After this you should have an IEnumerable<csvClass>() with just the 2 columns you want, which you can write to anywhere you want - new csv-file, database ...

    0 讨论(0)
提交回复
热议问题