【发布时间】: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