我有两个并发字典说
var MainDic = new ConcurrentDictionary<string, string>();
和
var TempDic = new ConcurrentDictionary<string, string>(MainDic);
MyTempDic
包含与 .I 相同的数据MainDic
。我在 .I 上进行计算TempDic
。所做的任何更改TempDic
都会反映在MainDic
. 我该如何阻止这种情况,我需要保持MainDic
原样以供进一步参考
以下是我的实际代码:
ConcurrentDictionary NetPositionData = new ConcurrentDictionary(); // 主目录
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 字典中。因此,我丢失了原始数据。我不希望这发生