我不認爲這是遍歷整個緩存反正一個偉大的想法,但你可以用類似做非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