LINQ Convert Dictionary to Lookup

前端 未结 4 1768
陌清茗
陌清茗 2020-12-16 11:57

I have a variable of type Dictionary>
I want to convert it to a Lookup.

<
4条回答
  •  猫巷女王i
    2020-12-16 12:43

    Not an answer for the question, but I think this is related information and should be posted here.

    There is some edge cases you should take into account. All of them about items of dictionary, which have key, but don't have value.

    This is expected behavior. Dictionary and Lookup designed for different purposes.

    var dic = new Dictionary> { [true] = null };
    var lookup = dic.ToLookup();
    
    Assert.AreEqual(1, dic.Count);
    Assert.AreEqual(0, lookup.Count);
    
    Assert.IsTrue(dic.ContainsKey(true));
    Assert.IsFalse(lookup.Contains(true));
    
    Assert.IsFalse(dic.ContainsKey(false));
    Assert.IsFalse(lookup.Contains(false));
    
    dic[false] -> Exception
    lookup[false] -> bool?[0]
    

提交回复
热议问题