2016-03-02 175 views
1

我的任務是讀取CSV文件並將文件內容放入字典中。將多個對象作爲值添加到字典中的鍵

問題是讀入的每行都會有一個字典中已經存在的鍵。我被告知要將每行讀取的值(來自類對象)添加到與該行相關的密鑰中,該行是帳戶。

所以文件樣品看起來像這樣在每一行,大約有10個賬戶,但8000線:

帳戶1,值1,值2,值3

帳戶1,值1,值2,值3

所以我被要求做的是,當存在一個已經存在的獨特密鑰時,我需要在該對象(每行讀入)中添加該值。

但是當我被要求用一個對象做這件事時,這真的讓我感到困惑。我明白字典的方式是,如果鍵/值對是這樣的(字符串,整數):

KEY1,5

KEY1,10

當我第二值添加到不同鍵它會看起來像這樣: key1,15 15

我沒有理解,當涉及到使用簡單的值,如int。

但是,如果我使用一個類,我的價值:

public class DataRecord 
    { 
     public string account; 
     public int open; 
     public int buy; 
     public int sell; 
     public double settleMM; 
     public string underlying; 
     public string symbol; 
    } 

這裏是我有我的應用程序迄今:

static void Main(string[] args) 
    { 
     double dParseSettleMM; 
     int iParseOpen; 
     int iParseBuy; 
     int iParseSell; 
     string sUnderlyingControl = string.Empty; 
     string sUnderlying = string.Empty; 
     string sAccount = string.Empty; 
     string sAccountControl = string.Empty; 

     var path = @"C:\Users\jhochbau\documents\visual studio 2015\Projects\CsvReader\CSVReaderList\Position_2016_02_25.0415.csv"; 

     Dictionary<string, DataRecord> vSummaryResults = new Dictionary<string, DataRecord>(); 

     //Sets up control to switch between sorted lists. 
     Console.WriteLine("Enter 1 to sort by Account, 2 to sort by Underlying, 3 to sort by Account and Underlying"); 
     string control = Console.ReadLine(); 

     //open reader 
     StreamReader r = new StreamReader(path); 
     StreamWriter vWriteFile = new StreamWriter("Positions2.csv"); 

     //Consume first line 
     r.ReadLine(); 

     //While loop to populate List with record Objects 
     while (!r.EndOfStream) 
     { 

      DataRecord records = new DataRecord(); 
      var line = r.ReadLine(); 
      var values = line.Split(','); 

      //Need to add into dictionary... 
      if (vSummaryResults.ContainsKey(records.account)) 
      { 
       vSummaryResults[records.account].open += records.open; 
      } 
      else 
      { 
       vSummaryResults.Add(records.account, records); 
      } 
     } 

    } 

編輯:這裏是我的具體問題。我被告知這段代碼已經有效,我想了解的是如何以及爲什麼,並嘗試將其視覺化以便理解它。

當我將其添加到字典中時,該類中每個字段發生了什麼?我的DataRecord類是否被用作值,只有這些字段的多個實例?

+1

,你應該換你流在usings讀寫器 –

+0

是,自動關閉功能:) –

+0

你打算合併數據結構呢?例如,一條記錄可能具有DataRecord.open的非默認值,另一條記錄可能具有DataRecord.symbol的非默認值?每條輸入行有三個值(或者僅僅是一個示例,而是DataRecord對象的整個圖像)? –

回答

3

使您的字典值爲DataRecords的List(或其他集合)。

Dictionary<string, List<DataRecord>> vSummaryResults = new Dictionary<string, List<DataRecord>>(); 

然後,您可以將在執行過程中找到的任何新值添加到該列表中。

選項1:我繼續並修改代碼來執行此操作。

static void Main(string[] args) 
{ 
    double dParseSettleMM; 
    int iParseOpen; 
    int iParseBuy; 
    int iParseSell; 
    string sUnderlyingControl = string.Empty; 
    string sUnderlying = string.Empty; 
    string sAccount = string.Empty; 
    string sAccountControl = string.Empty; 

    var path = @"C:\Users\jhochbau\documents\visual studio 2015\Projects\CsvReader\CSVReaderList\Position_2016_02_25.0415.csv"; 

    Dictionary<string, List<DataRecord>> vSummaryResults = new Dictionary<string, List<DataRecord>>(); 

    //Sets up control to switch between sorted lists. 
    Console.WriteLine("Enter 1 to sort by Account, 2 to sort by Underlying, 3 to sort by Account and Underlying"); 
    string control = Console.ReadLine(); 

    //open reader 
    StreamReader r = new StreamReader(path); 
    StreamWriter vWriteFile = new StreamWriter("Positions2.csv"); 

    //Consume first line 
    r.ReadLine(); 

    //While loop to populate List with record Objects 
    while (!r.EndOfStream) 
    { 

     DataRecord records = new DataRecord(); 
     var line = r.ReadLine(); 
     var values = line.Split(','); 

     //Need to add into dictionary... 
     if (vSummaryResults.ContainsKey(records.account)) 
     { 
      vSummaryResults[records.account].Add(record); 
     } 
     else 
     { 
      vSummaryResults.Add(records.account, new List<DataRecord>()); 
      vSummaryResults[records.account].Add(record); 
     } 
    } 

} 

選項2:由於您不能使用選項1.這將序列化每個記錄並將其附加到該鍵的值。然後你可以用','來分割它,然後反序列化它。上述方法更好,但如果你不能使用它,那麼這應該工作。

static void Main(string[] args) 
{ 
    double dParseSettleMM; 
    int iParseOpen; 
    int iParseBuy; 
    int iParseSell; 
    string sUnderlyingControl = string.Empty; 
    string sUnderlying = string.Empty; 
    string sAccount = string.Empty; 
    string sAccountControl = string.Empty; 

    var path = @"C:\Users\jhochbau\documents\visual studio 2015\Projects\CsvReader\CSVReaderList\Position_2016_02_25.0415.csv"; 

    Dictionary<string, string> vSummaryResults = new Dictionary<string, string>(); 

    //Sets up control to switch between sorted lists. 
    Console.WriteLine("Enter 1 to sort by Account, 2 to sort by Underlying, 3 to sort by Account and Underlying"); 
    string control = Console.ReadLine(); 

    //open reader 
    StreamReader r = new StreamReader(path); 
    StreamWriter vWriteFile = new StreamWriter("Positions2.csv"); 

    //Consume first line 
    r.ReadLine(); 

    //While loop to populate List with record Objects 
    var serializer = new JavaScriptSerializer(); 
    while (!r.EndOfStream) 
    { 

     DataRecord records = new DataRecord(); 
     var line = r.ReadLine(); 
     var values = line.Split(','); 

     //Need to add into dictionary... 
     if (vSummaryResults.ContainsKey(records.account)) 
     { 
      vSummaryResults[records.account] += "," + serializer.Serialize(record); 
     } 
     else 
     { 
      vSummaryResults.Add(records.account, serializer.Serialize(record)); 
     } 
    } 

} 
+0

希望我可以,但我被告知不要那樣做,否則這會容易得多! –

+0

下面是.NET中序列化程序的鏈接。 Newtonsoft也是一個選項。 https://msdn.microsoft.com/zh-cn/library/system.web.script.serialization.javascriptserializer(v=vs.110).aspx – Josh

+1

今天早上我開始工作,他們要求我現在使用Option非常感謝你發佈了這個建議。 –

相關問題