【发布时间】:2016-02-14 13:37:56
【问题描述】:
我无法使用字符串键获取值
...
Dictionary<string, Data> dateDic = new Dictionary<string, Data>();
...
public void GetDataList(string _code, int _startDate, int _limit, out List<Data> _list)
{
_list = (from data in dateDic[_code].Values // <= System.Collections.Generic.KeyNotFoundException!!!
where data.date >= startDate
orderby data.date descending
select data).Take(_limit).ToList<Data>();
}
变量_code 是027410
在观察窗口:
stockShcodeDic[_code] System.Collections.Generic.KeyNotFoundException
【问题讨论】:
-
你确定只有
027410。该字符串可能包含空字符\0但它不会出现 -
您在代码中使用
dateDic。稍后您参考stockShcodeDic["027410"]可以。那些字典是一样的吗? -
显然字典中没有
_code。当您遇到异常时,请在 Watch window_code == "027410"尝试。 -
在观察窗口,code == "027410" 是假的我发现我的错误
标签: c# dictionary keynotfoundexception