2014-02-10 50 views
2

我有兩個並行的字典說更新反映在主詞典

var MainDic = new ConcurrentDictionary<string, string>(); 

var TempDic = new ConcurrentDictionary<string, string>(MainDic); 

TempDic包含相同的數據,MainDic。我做TempDic計算。無論對TempDic所做的更改都反映在MainDic中。如何阻止這一點,我需要保持MainDic因爲它是爲進一步參考

以下是我的實際代碼:

ConcurrentDictionary NetPositionData =新ConcurrentDictionary(); // Main Dic

private DataView GetNetPositionData() 
    { 
     this.NetPosition.Tables[0].Rows.Clear(); 
     DataView view = new DataView(); 
     ConcurrentDictionary<string, DataNetPosition> Postion; 
     if (NetPosFlag == "A") 
     { 
      foreach (KeyValuePair<string, DataNetPosition> entry in NetPositionData) 
      { 
       this.NetPosition.Tables[0].Rows.Add(entry.Value.Exchange, entry.Value.SecurityId, entry.Value.ClientId, entry.Value.LTP); 
      } 
     } 
     else 
     { 
      Postion = new ConcurrentDictionary<string, DataNetPosition>(GetDayPosition(NetPositionData)); 
      foreach (KeyValuePair<string, DataNetPosition> entry in Postion) 
      { 
       this.NetPosition.Tables[0].Rows.Add(entry.Value.Exchange, entry.Value.SecurityId, entry.Value.ClientId, entry.Value.LTP); 
      } 
     } 
     return view; 
    } 

    private ConcurrentDictionary<string, DataNetPosition> GetDayPosition(ConcurrentDictionary<string, DataNetPosition> _ALLPos) 
    { 
     var _DayPos = new ConcurrentDictionary<string, DataNetPosition>(_ALLPos); 
     try 
     { 
      DataView dv = new DataView(CFnetposition.Tables[0]); 
      for (int i = 0; i < dv.Table.Rows.Count; i++) 
      { 
       string NKey = dv.Table.Rows[i]["Exchange"].ToString() + dv.Table.Rows[i]["SecurityId"].ToString() + dv.Table.Rows[i]["ClientID"].ToString() + dv.Table.Rows[i]["Product"].ToString(); 
       if (_DayPos.ContainsKey(NKey)) 
       { 
        var dnp = _DayPos[NKey]; 
        if (dv.Table.Rows[i]["Buy/Sell"].ToString() == "Buy") 
        { 
         dnp.BuyQuantity = dnp.BuyQuantity - Convert.ToDouble(dv.Table.Rows[i]["Quantity"]); 
         dnp.BuyVal = dnp.BuyVal - Convert.ToDouble(dv.Table.Rows[i]["TradeValue"]); 
        } 
        else 
        { 
         dnp.SellQuantity = dnp.SellQuantity - Convert.ToDouble(dv.Table.Rows[i]["Quantity"]); 
         dnp.SellVal = dnp.SellVal - Convert.ToDouble(dv.Table.Rows[i]["TradeValue"]); 
        } 

        dnp.BuyAvg = dnp.BuyQuantity == 0 ? 0 : dnp.BuyVal/dnp.BuyQuantity; 
        dnp.SellAvg = dnp.SellQuantity == 0 ? 0 : dnp.SellVal/dnp.SellQuantity; 
        dnp.NetQuantity = dnp.BuyQuantity - dnp.SellQuantity; 
        // other caluculations 
        _DayPos.TryUpdate(NKey, dnp, null); 
       } 
      } 
     } 
     catch (Exception ex) 
     { 

     } 
     return _DayPos; 
    } 

這裏如果標誌是A我返回數據,因爲它是其他我調用GetDayPosition。在GetDayPosition函數中,我在_DayPos中做的任何更新都反映在NetPositionData字典中。因爲這我失去了我的原始數據。我不希望發生這種情況

+0

你所說的 「計算」 是什麼意思?請你能包括一個例子,一個小腳本,顯示你做什麼,你想要什麼。 – Aron

+0

我已經添加了我的代碼,請參閱並讓我知道我在做什麼錯誤。 –

+0

您需要了解'deep-copy'和'shallow-copy'之間的區別。 'var TempDic = new ConcurrentDictionary (MainDic)'就是我們所說的'淺拷貝',你需要爲你的目的做一個'深拷貝'。 – Aron

回答

0

你確定嗎?

 var mainDic = new ConcurrentDictionary<string, string>(); 
     mainDic["1"] = "foo"; 

     var tempDic = new ConcurrentDictionary<string, string>(mainDic); 
     tempDic["1"] = "bar"; 

     Console.Out.WriteLine(mainDic["1"]); 

輸出 - >富

+0

我懷疑OP是簡化了他們的例子。我懷疑所討論的類型是可變引用類型,問題不在於字典中,而是兩個字典都持有對同一對象的引用。 – Aron