0

我在这里有点困惑。我收到了这个异常,它告诉我这个密钥不存在。当我在监视窗口中查看字典中的所有键/值对时,有问题的键就在那里!!!我不认为我会发疯,但我可能会发疯。我在这里错过了什么吗?这是代码:

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
}

更新:

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

1 回答 1

2

尝试遍历字典中的值/对。看看你是否真的有这个元素(眼睛会说谎)。

foreach (var pair in ackData)
{
    Console.WriteLine("{0},{1}", pair.Key, pair.Value);
}
于 2012-05-17T18:51:47.953 回答