Обновление в одном Concurrentdictionary отражается в основном словаре

У меня есть два словарей одновременно

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

а также

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

мой TempDic содержит те же данные, что и MainDicЯ делаю вычисления на TempDic, все изменения сделаны TempDic отражается в MainDic, Как мне это остановить, мне нужно сохранить MainDic как это для дальнейшего использования

Вот мой фактический код:

ConcurrentDictionary NetPositionData = new 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. из-за этого я теряю свои исходные данные. Я не хочу, чтобы это случилось

1 ответ

Вы уверены, что?

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

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

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

выходы -> foo

Другие вопросы по тегам