C# SortedDictionary 产生异常结果 - Mk2

发布于 2024-11-15 04:57:14 字数 1159 浏览 3 评论 0原文

我认为我在上一个问题中没有发布足够的细节,人们似乎停止回应,所以我重新发布,因为我们需要知道为什么会发生这个问题

我正在使用 SortedDictionary,当我们循环它时,我们会感到奇怪结果。

涉及很多嵌套,最终的字典实际上是另一个字典的子字典,而另一个字典又是另一个字典的子字典!

是完整的巢。

SortedDictionary<String, SortedDictionary<String, SortedDictionary<int, SortedDictionary<String, String>>>>()

我正在循环的字典是

SortedDictionary<int, SortedDictionary<String, String>>

这是循环:

foreach (SortedDictionary<String, String> cDic in openTrades.Values)
{
    String cTimestamp = convertTimestamp(cDic["open"]);
    if (!closeTrades.ContainsKey(cDic["key"]) && barArray.ContainsKey(cDic["pair"]))
    {
          foreach (SortedDictionary<String, String> bDic in barArray[cDic["pair"]][cDic["frame"]].Values)
          {
               //This is the relative Loop
          }
    }
}

barArray 是我们的 Primary SortedDictionary (这个问题的主题) openTrades 是另一个 SortedDictionary

现在,当我们以整数作为索引循环遍历字典时,我们会得到不同的结果 - IE。如果我们在循环时将 1,2,3,4 作为键,则可能会按以下顺序显示它们:4,2,1,3,这显然没有意义,因为这意味着这是一个排序的字典。

任何尽快的帮助将不胜感激,因为我对这个问题感到困惑。 谢谢 詹姆斯

I don't think I posted enough detail in the previous question and people seemed to stop responding so I'm reposting as we need to know why this problem is happening

I'm working with a SortedDictionary and when we loop through it we get odd results.

there is a lot of nesting involved and the final dictionary is actually the child of another dictionary which is the child of another!

is the the complete nest.

SortedDictionary<String, SortedDictionary<String, SortedDictionary<int, SortedDictionary<String, String>>>>()

The dictionary i'm looping through is

SortedDictionary<int, SortedDictionary<String, String>>

And Here is the loop:

foreach (SortedDictionary<String, String> cDic in openTrades.Values)
{
    String cTimestamp = convertTimestamp(cDic["open"]);
    if (!closeTrades.ContainsKey(cDic["key"]) && barArray.ContainsKey(cDic["pair"]))
    {
          foreach (SortedDictionary<String, String> bDic in barArray[cDic["pair"]][cDic["frame"]].Values)
          {
               //This is the relative Loop
          }
    }
}

barArray is our Primary SortedDictionary (the subject of this question)
openTrades is another SortedDictionary

Now when we loop through the dictionary with an integer as an index we get varied results - IE. if we have 1,2,3,4 as the keys when looping through it may present them in this order: 4,2,1,3 which clearly doesn't make sense as this is meant to be a sorted dictionary.

Any help as quickly as possible would be greatly appreciated as im stumped on this issue.
Thanks
James

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

§对你不离不弃 2024-11-22 04:57:14

我的直觉是你误解了你所看到的结果。在您显示的循环中,您不是循环遍历 int 键,而是循环遍历值列表。因此,您无法确定 int 键实际返回的顺序。

您可能在 SortedDictionary 本身中有一些内容,应该指示它属于封闭字典中的哪个 int 。我想,您形成这些值的方式存在一些错误,因此键中的 int 实际上与该值不匹配。

我建议缩小问题范围,通过 openTrade 进行 foreach,而不是 openTrades.Values。这样您就可以在返回的 KeyValuePair 中看到真实的键。我很确定它们会出现排序。

My gut feeling is that you are misinterpreting the results that you are seeing. In the loop that you show, you are not looping through the int key, you are looping through the list of values. Thus, you have no way to determine in which order the int keys are actually returned.

You probably have something in the SortedDictionary itself, that should indicate which int from the enclosing dictionary it belongs to. I would imagine, that there is some bug in how you are forming these values, so that the int in the key does not actually match the value.

I suggest to narrow the issue down, that you foreach through openTrades and not openTrades.Values. This way you'll be able to see the real keys in the returned KeyValuePair. I'm pretty sure they will appear sorted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文