title: Slicing a FormCollection by keys that start with a certain string
body: <p>Is there a nice <em>linqy</em> way of splitting a <code>FormCollection</code> into a <code>Dictionary&lt;string,string&gt;</code> that contains only those keys that start with a certain string?</p>
  
  <p><em>(This question is basically the same as this-> but for C#/FormCollection instead of python <a href="https://stackoverflow.com/questions/4558983/slicing-a-dictionary-by-keys-that-start-with-a-certain-string">Slicing a dictionary by keys that start with a certain string</a>)</em></p>
  
  <p>Here's what I came up with to get around the problem: </p>
  
  <pre><code>public ActionResult Save(FormCollection formCollection) {
    var appSettings = new Dictionary&lt;string, string&gt;();
    var appKeys = formCollection.AllKeys.Where(k =&gt; k.StartsWith("AppSettings."));
    foreach (var key in appKeys)
    {
        appSettings[key] = formCollection[key];
    }
  ...
  </code></pre>
  
  <p>Edit: The problem with this code, is that I have to do it multiple times for different StartsWith strings, and will therefore need to create a 'utility' method to do the above.
  It would be nice if it could read in one line like: </p>
  
  <pre><code>formCollection.Where(k=&gt;k.Key.StartsWith("AppSettings.");
  </code></pre>
  
  <p><strong>Background (not necessary to solve the problem):</strong> The context is asp.net mvc, and of a form with a dynamic dictionary of fields.</p>
  
  <p>It's also similar to this question - <a href="https://stackoverflow.com/questions/1109796/return-formcollection-items-with-prefix">Return FormCollection items with Prefix</a> - but not quite the same.  </p>
  
  <p>And having read this answer <a href="https://stackoverflow.com/questions/2845304/how-to-build-c-object-from-a-formcollection-with-complex-keys">How to build C# object from a FormCollection with complex keys</a> - I started to wonder whether I'd be better off not even using form post, but sending JSON instead.</p>