2013-10-16 70 views
0

背景:我試圖從包含字符串和雙打組合的csv文件讀取。我想離開字符串,因爲它們用於搜索和索引目的,但能夠對列表中的double值執行數據分析。我也不想將整個csv文件讀入內存,然後再將它分離到不同的類別中,所以我試圖將它分配給自定義數據類型的列表,因爲它是從csv讀取的。如何將字符串列表轉換爲自定義數據類型?

如何將單個列表條目轉換爲雙精度型,然後將其添加到列表中?

class data 
{ 
    public List<string> timefrom { get; set; } 
    public List<string> timeto { get; set; } 
    public List<string> type { get; set; }   
    public List<string> site { get; set; } 
    public List<string> box { get; set; } 
    public List<string> group { get; set; }   
    public List<string> sourcecopy { get; set; } 
    public List<string> destcopy { get; set; } 
    public List<string> gridid { get; set; }   
    public List<string> stat { get; set; } 
    public List<double> value { get; set; } 
    public List<string> unit { get; set; } 
    public List<string> peak { get; set; } 
} 

class Class1 
{ 
    public List<data> 
     WANtpfromSite, 
     WANtpbyCG, 
     IncomingWritesbyCG, 
     LinkUtilbyCG, 
     BoxUtil, 
     CompressionCPU, 
     ReplicationCPU, 
     CompressionRatio, 
     DeduplicationRatio, 
     IncomingWritesbyBox, 
     IncomingWritesbyBoxReplicating, 
     IObyBox, 
     IObyBoxReplicating, 
     PacketLoss, 
     LineLatency; 


    public void getDayData(string directory) 
    { 

     string[] fileEntries; 

     fileEntries = System.IO.Directory.GetFiles(directory + "/home/www/info/long_term_stats"); 
     string filePath = fileEntries[0]; 
     System.IO.StreamReader sr = new System.IO.StreamReader(filePath); 
     List<string> Line; 
     int row = 1; 
     while (!sr.EndOfStream) 
     { 
      //Splits the line read into a list of string values 
      Line = sr.ReadLine().Split(',').ToList<string>(); 

      // Here I'll probably use a switch case to set the variable, but this is an example of what it should do. 
      if (Line[8] == "Wan Throughput from site") 
      { 
       //Here is where I'm having the problem - it can't implicitely convert the string list to a data list because 
       // of that single double value within the list. How would I go about explicitely converting the "Line" 
       //variable to a <data> type? 

       WANtpfromSite = List <data> datatype; 
      } 
      row++; 

     } 

    } 
+0

如果有一種數據類型可以支持這個功能,或者我不知道的免費庫,請告訴我!我對C#相當陌生(在MATLAB中扼殺了我的牙齒,並且它很容易轉換 - 數據類型的輝煌 - 所以這類問題對我來說是新的) – bubthegreat

回答

0

好吧 - 我看起來好像看起來像一組DataTable元素就是我所需要的。而不是做一個列表清單,我只是爲每個變量類型設置一個數據表。

相關問題