[英]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.