【问题标题】:Get value of an OrderedDictionary获取 OrderedDictionary 的值
【发布时间】:2021-03-20 14:06:59
【问题描述】:

我无法从我的 OrderedDictionary 中找到值,我正在关注堆栈上的几个帖子,所以我看不出我做的不好?

private List<IfcElement> readListParamFromList(Int32 index, OrderedDictionary ifcList)
{
    List<IfcElement> retour = new List<IfcElement>();
    if (this.ListParams[index] != "")
    {
        string[] listStrings = this.ListParams[index].Split(',');

        foreach (string idString in listStrings)
        {
            long idElt = -1;
            idElt = IfcGlobal.GetIdFromIfcString(idString);
            try
            {
                object objectFound = (IfcElement)ifcList[(object)idElt];
                IfcElement newElt = (IfcElement)objectFound;
                retour.Add(newElt);
                this.addChildren(newElt);
            }
            catch
            {
                this.ErrorFound = true;
                this.ListItemsNotFound.Add(idElt);
            }
        }
    }
    return retour;
}

在这里我找到 IdElt=104。然后我在调试中检查,我的 OrderedDictionary 里面有一个 Key=104 的元素,并且对象存在于值中,但是 object objectFound = (IfcElement)ifcList[(object)idElt]; 行总是返回 null。我的语法有问题吗? 也许它有帮助,我添加了我在字典中添加元素的方式:

public class GroupedListElements
{
    public string Type { get; set; } = "";
    public OrderedDictionary ListIfcElements = new OrderedDictionary();
    public GroupedListElements()
    {

    }
}

GroupedListElements newGroupList = new GroupedListElements { Type = 

newElt.GetType().Name };
newGroupList.ListIfcElements.Add(newElt.ID, newElt);
this.ListGroupped.Add(newGroupList);

【问题讨论】:

  • OrderedDictionary 中 Key=104 的类型是什么?是长的还是字符串的?
  • @user2250152 很长
  • @user2250152 试图更改我的字典,并在我的键中只放入字符串,但没有任何变化
  • 倒数第二行声明的newElt.ID类型是什么?
  • 很长,但是在user2250152的评论之后,也尝试全部添加为字符串Add(newElt.ID.ToString(),newElt),但仍然是同样的问题,如果我使用函数myDictionary.Contains(idElt.ToString())也返回false

标签: c# ordereddictionary


【解决方案1】:

我认为问题在于,通过在从有序字典中检索对象之前强制转换为 (object),字典会尝试根据 Object.Equals() 定位键。如果装箱的长对象具有相同的引用,则 Object.Equals 返回 true(即 ReferenceEquals 方法返回 true)。如果您不介意使用字符串而不是 long 作为键,我建议您这样做。

要详细了解您的代码出了什么问题,可以替换以下行

object objectFound = (IfcElement)ifcList[(object)idElt];

object objectKey = (object)idElt;
object objectFound = (IfcElement)ifcList[objectKey];

然后在调试器的即时窗口中查看 objectKey.ReferenceEquals(x) 是否对任何 x in 都返回 true

ifcList.Keys

【讨论】:

  • 实际上我尝试用字符串替换所有内容,但我遇到了同样的问题。一些奇怪的事情(或者我不明白),如果我使用 Dictionary&lt;double,MyObject&gt; 一切正常,但使用 OrderedDictionary 不是。
  • @Siegfried.V: 还有一个通用的 SortedDictionary
  • @Siegfried V. SortedDictionary 将完全按照您的意愿进行操作:无论按什么顺序填充,它都会自动按 de 键值排序。当然,这仅在您通过其枚举器(foreach ...)访问字典时才相关。如果您通过它的键从字典中检索元素,则排序是无关紧要的(这是字典的全部意义)。
  • @Siegfried.V 由于 Dictionary 通过其键的哈希码提供对任何元素的直接访问,因此只要您拥有大量元素,它就应该胜过 List 中的二进制搜索。两者之间有很多比较,例如dotnetperls.com/dictionary-timetheburningmonk.com/2011/03/hashset-vs-list-vs-dictionary
  • Siegfried.V 除了访问时间之外要考虑的一件事是字典不能有重复项(即没有重复键)。一个列表可以。因此,如果您连接两个列表,您最终可能会得到两个具有相同键值的元素。关于此的有趣帖子:stackoverflow.com/questions/294138/…
猜你喜欢
  • 2018-06-11
  • 2013-03-23
  • 1970-01-01
  • 2017-04-05
  • 2011-01-14
  • 2013-05-17
  • 2016-02-04
  • 1970-01-01
  • 2012-10-05
相关资源
最近更新 更多