简体   繁体   English

C#中字典中的KeyNotFoundException

[英]KeyNotFoundException in dictionary in C#

I'm a bit baffled here. 我在这里有点困惑。 I'm getting this exception which is telling me this key doesn't exist. 我收到这个异常,告诉我这个密钥不存在。 When I look at all the key/value pairs in the dictionary in the watch window, the key in question IS THERE!!! 当我在观察窗口中查看字典中的所有键/值对时,问题的关键在于!!! I don't think I'm going crazy, but I could be. 我不认为我会发疯,但我可能会。 Am I missing something here? 我在这里错过了什么吗? Here's the code: 这是代码:

public static List<Quote> GetQuoteHistory(int id)
{
    List<Quote> quotes = DataAccess.GetQuoteHistory(id);
    SetAckCodeDescriptions(quotes);
    return quotes;
}

private static void SetAckCodeDescriptions(IEnumerable<Quote> quotes)
{
    Dictionary<string, string> ackData = GetAcknowlegementData();

    foreach (Quote quote in quotes)
    {
        quote.AckCodes = GetAckCodeHtml(quote.AckCodes, ackData);
    }
}

private static string GetAckCodeHtml(string ackCodes, IDictionary<string, string> ackData)
{
    string[] codes = ackCodes.Split(',');

    string html = string.Empty;
    foreach (string code in codes)
    {
        html += string.Format("<div title='#= {0} #'>#= {1} #</div>, ", ackData[code], code);
    }

    return html.TrimEnd(new []{',', ' '}); // remove extra comma and space
}

UPDATE: 更新:

public static Dictionary<string, string> GetAcknowlegementData()
{
    List<AckData> list = DataAccess.GetAcknowlegementData();
    return list.ToDictionary(o => o.AcknowledgementCode, o => o.Description);
}

Try to iterate over the value/pair in your dictionary. 尝试迭代字典中的值/对。 See if you have actually the element (eyes can lie). 看看你是否真的有这个元素(眼睛可以撒谎)。

foreach (var pair in ackData)
{
    Console.WriteLine("{0},{1}", pair.Key, pair.Value);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#:KeyNotFoundException:字典中不存在给定的键 - C#: KeyNotFoundException: The given key was not present in the dictionary C#嵌套字典-字典中的字典返回错误(KeyNotFoundException) - c# nested dictionary - dictionary in a dictionary return error (KeyNotFoundException) 当键存在时,C#字典会抛出KeyNotFoundException - C# Dictionary throws KeyNotFoundException when key exists Xamarin.Forms - KeyNotFoundException: 字典中不存在给定的键 C# - Xamarin.Forms - KeyNotFoundException: The given key was not present in the dictionary C# KeyNotFoundException:字典中不存在给定的键(C#Unity ANDROID APP) - KeyNotFoundException: The given key was not present in the dictionary (C# Unity ANDROID APP) C#AutoMapperMappingException KeyNotFoundException - C# AutoMapperMappingException KeyNotFoundException C#中的KeyNotFoundException - KeyNotFoundException in C# MySql Query / C#上的KeyNotFoundException - KeyNotFoundException on MySql Query / C# Unity C#应用程序KeyNotFoundException - Unity C# application KeyNotFoundException C#Dictionary中的KeyNotFoundException在根据内容更改属性值后,计算GetHashCode。 为什么? - KeyNotFoundException in C# Dictionary after changing property value based on what, the GetHashCode is calculated. Why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM