Tech Off Post

Single Post Permalink

View Thread: Can you swap nested dictionaries keys in LINQ without taking huge perf hit?
  • User profile image
    androidi

    My current code is this. Of course I could write some other code so that this isn't even needed but regardless I'm interested in how to do this in LINQ while preserving similar perf.

            private static void SwapDictKeys(Dictionary<DateTime, Dictionary<MyKey, MyValue>> dorg, Dictionary<MyKey, Dictionary<DateTime, MyValue>> dswp)
            {
                foreach (var dt in dorg)
                {
                    foreach (var mval in dt.Value)
                    {
                        Dictionary<DateTime, MyValue> dhlp;
                        if (!dswp.TryGetValue(mval.Key, out dhlp)) { dhlp = new Dictionary<DateTime, MyValue>(); dswp.Add(mval.Key, dhlp); }
                        dhlp.Add(dt.Key, mval.Value);
                    }
                }
            }