我不认为这是遍历整个缓存反正一个伟大的想法,但你可以用类似做非LINQ:
var iter = HttpContext.Current.Cache.GetEnumerator();
using (iter as IDisposable)
{
while (iter.MoveNext())
{
string s;
if ((s = iter.Key as string) != null && s.Contains("subcat"))
{
//... let the magic happen
}
}
}
与LINQ做到这一点,你可以做是这样的:
public static class Utils
{
public static IEnumerable<KeyValuePair<object, object>> ForLinq(this IDictionaryEnumerator iter)
{
using (iter as IDisposable)
{
while (iter.MoveNext()) yield return new KeyValuePair<object, object>(iter.Key, iter.Value);
}
}
}
和使用,如:
var items = HttpContext.Current.Cache.GetEnumerator().ForLinq()
.Where(pair => ((string)pair.Key).Contains("subcat"));
撇开:枚举整个缓存本来就是一个在繁忙的网站上的一个坏主意...... – 2011-02-01 11:54:22
是的,我听到你们所有......回到绘图板 – leen3o 2011-02-01 12:33:36