title: Find key using lambda expression in HttpContext.Current.Items cache
body: <p>I have a cache by request, implemented using HttpContext.Current.Items like this:</p>
  
  <pre><code>private static readonly Lazy&lt;CacheCurrentCall&gt; lazy =
      new Lazy&lt;CacheCurrentCall&gt;(() =&gt; new CacheCurrentCall());
  
  public static CacheCurrentCall Instance
  {
      get
      {
          IDictionary items = HttpContext.Current.Items;
          if (!items.Contains("CacheCurrentCall"))
          {
              items["CacheCurrentCall"] = new CacheCurrentCall();
          }
          return items["CacheCurrentCall"] as CacheCurrentCall;
      }
  }
  
  private CacheCurrentCall()
  {
  
  }
  
  public void Add&lt;T&gt;(T o, string key, int cacheDurationSeconds = 0)
  {
      HttpContext.Current.Items.Add(key, o);
  }
  
  public void Clear(string key)
  {
      HttpContext.Current.Items.Remove(key);
  }
  
  public bool Exists(string key)
  {
      return HttpContext.Current.Items[key] != null;
  }
  
  public bool Get&lt;T&gt;(string key, out T value)
  {
      try
      {
          if (!Exists(key))
          {
              value = default(T);
              return false;
          }
          value = (T)HttpContext.Current.Items[key];
      }
      catch
      {
          value = default(T);
          return false;
      }
      return true;
  }
  </code></pre>
  
  <p>Now I would need to remove all of the keys starting by a particular string, and therefore I was thinking of a method like this</p>
  
  <pre><code>public IEnumerable&lt;string&gt; GetKey (Func&lt;string, bool&gt; condition)
  </code></pre>
  
  <p>and then looping through the results and clear them (I could even clear directly in a Clear acepting a lambda expression, I guess). But I'm lost trying to implement such a method, if it's possible actually.</p>
  
  <p>Any help?</p>
  
  <p>Thanks</p>
  
  <p>Edit:</p>
  
  <p>Servy, I was trying (have been blindly trying some things but more or less following this path)</p>
  
  <pre><code>public IEnumerable&lt;string&gt; GetKeys(Func&lt;string, bool&gt; condition)
  {
      List&lt;string&gt; list = new List&lt;string&gt;();
  
      foreach (var key in HttpContext.Current.Items.Keys)
      {
          if (condition(key as string))
          {
              list.Add(key as string);
          }
      }
  
      return list;
  }
  </code></pre>
  
  <p>But I'm getting:</p>
  
  <p>Object reference not set to an instance of an object</p>
  
  <p>I'm gonna try right now p.s.w.g that besides it probably works it's much more elegant to my eyes.</p>
  
  <p>Second edit:</p>
  
  <p>I needed to change slightly p.s.w.g solution. I don't store strings in the cache but other kind of objects, so I use this now</p>
  
  <pre><code>public IEnumerable&lt;string&gt; GetKeys (Func&lt;string, bool&gt; condition)
  {
      return HttpContext.Current.Items
          .Cast&lt;DictionaryEntry&gt;()
          .Where(e =&gt; e.Key is string &amp;&amp; condition(e.Key as string))
          .Select(e =&gt; e.Key as string);
  }
  </code></pre>
  
  <p>And a call to clear the cache is for example this one</p>
  
  <pre><code>public void ClearCache()
  {
      var ownedItemSummaryKeys = CacheCurrentCall.Instance.GetKeys(k =&gt; k.Contains("OwnedItemSummaryCurrent"));
  
      foreach (var ownedItemSummaryKey in ownedItemSummaryKeys.ToList())
      {
          CacheCurrentCall.Instance.Clear(ownedItemSummaryKey);
      }
  }
  </code></pre>