How do I delete certain column from .csv file

核能气质少年 提交于 2019-12-01 14:21:08

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 ...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!