How to add item into c# dictionary like we do in python?

in python this works fine for me

ltt_min_1=datetime.datetime(ltt.year, ltt.month,, ltt.hour,ltt.minute)
candles_1[instrument][ltt_min_1]["high"] = max(candles_1[instrument][ltt_min_1]["high"],tick["last_price"])  # 1
candles_1[instrument][ltt_min_1]["low"] = min(candles_1[instrument][ltt_min_1]["low"],tick["last_price"])  # 2

candles_1[instrument][ltt_min_1]["close"] = tick["last_price"]  # 3

same thing i want to do in c# i tried like below

 Dictionary<string, decimal> candles_1= new Dictionary<string, decimal>();
 foreach (var TickData in collection)

  var instrument = TickData.InstrumentToken;
  var ltt = TickData.LastTradeTime.HasValue ? TickData.LastTradeTime.Value : DateTime.Now;
  var ltt_min_1 = new DateTime(ltt.Year, ltt.Month, ltt.Day, ltt.Hour, ltt.Minute,ltt.Second);

but not sure how to compare and add items into dict. may be linq not very sure pls guide. thanks

  • updated details so idea is i have bunch of data coming into collection and want to compare if data is within current minute then i compare and replace values of open,high,low,close into dictionary that is what i'm doing using python code works good for me now same want to do it using c# code.

1 answer

  • answered 2020-10-16 05:03 Mahnoor Khan

    Dictionary<string, Int16> AuthorList = new Dictionary<string, Int16>();
    AuthorList.Add("Mahesh Chand", 35);