我遇到了一些麻烦,并且不太明白为什么我无法从存储在下面代码中的集合中获取特定值...
public Dog Get(string name)
{
Dog dog = null;
var context = HttpContext.Current;
if (context.Cache[CacheKey] != null)
{
System.Collections.IDictionaryEnumerator en = context.Cache.GetEnumerator();
while (en.MoveNext())
{
if (en.Key.ToString() == "AnimalStore")
{
// I would like to use a foreach loop to look for a specific dog here but I get an error!
// foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'
foreach (var item in en.Value)
{
// en.Value contains two Dog objects with data but I can't get at them or their properties....
}
}
}
//dog = (Dog)context.Cache[CacheKey];
}
return dog;
}
foreach 语句不能对“System.Collections.IDictionaryEnumerator”类型的变量进行操作,因为“System.Collections.IDictionaryEnumerator”不包含“GetEnumerator”的公共定义
其他人可以解释为什么我不能循环遍历我的数组来获取 Dog.name 并执行以下操作: //return dogs.Find(p => p.name == name); 这对我来说是一个相当令人困惑的概念,所以我很感激理解方面的帮助......