2

为什么即使字典在调试时显示正确的键/值对,它也没有返回正确的字符串?

IDictionary<string, string> states = 
                   AFS.MvcApplication.UnitedStates.StateDictionary;

string stateAbbrev = 
               states.Where(x => x.Key == State).Select(x => x.Value).ToString();
4

2 回答 2

3

你应该使用:

string stateAbbrev =  states[State]; 
于 2012-07-10T16:59:39.490 回答
2

您想使用FirstOrDefault而不是ToString,因为 的结果SelectIEnumerable<string>。这应该有效:

string stateAbbrev = states.Where(x => x.Key == State).Select(x => x.Value).FirstOrDefault();
于 2012-07-10T16:53:22.677 回答