7

Is there a nice linqy way of splitting a FormCollection into a Dictionary<string,string> that contains only those keys that start with a certain string?

(This question is basically the same as this-> but for C#/FormCollection instead of python Slicing a dictionary by keys that start with a certain string)

Here's what I came up with to get around the problem:

public ActionResult Save(FormCollection formCollection) {
  var appSettings = new Dictionary<string, string>();
  var appKeys = formCollection.AllKeys.Where(k => k.StartsWith("AppSettings."));
  foreach (var key in appKeys)
  {
      appSettings[key] = formCollection[key];
  }
...

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:

formCollection.Where(k=>k.Key.StartsWith("AppSettings.");

Background (not necessary to solve the problem): The context is asp.net mvc, and of a form with a dynamic dictionary of fields.

It's also similar to this question - Return FormCollection items with Prefix - but not quite the same.

And having read this answer How to build C# object from a FormCollection with complex keys - I started to wonder whether I'd be better off not even using form post, but sending JSON instead.

Community
  • 1
  • 1
PandaWood
  • 8,086
  • 9
  • 49
  • 54
  • What is wrong with the code you have? – The Scrum Meister Feb 03 '11 at 23:58
  • Note that you should always describe the *problem*. Here it sounds like you're saying "I'm doing something, and this is how I'm doing it. Is there a nice way of doing this?", yet not telling us why your current solution is *not nice*. – Lasse V. Karlsen Feb 04 '11 at 00:03
  • Thanks, I've edited the question to say why it's not nice (with the word "Edit" so it doesn't look like you're complaining about nothing) – PandaWood Feb 04 '11 at 00:13
  • See my answer, and you can always just make that 1 line if needs be. Having said that, making a utility method is probably not the worst idea anyway :) – Lasse V. Karlsen Feb 04 '11 at 00:29

2 Answers2

17

If you're looking for a "nice" way of taking an existing dictionary, producing a new dictionary with copies of keys+values, for a subset of the keys, some LINQ code will do this nicely:

var appSettings = formCollection.AllKeys
    .Where(k => k.StartsWith("AppSettings."))
    .ToDictionary(k => k, k => formCollection[k]);
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • The only problem is the MVC 'FormCollection', which only implements NameValueCollection - which I should have mentioned/realized doesn't implement IEnumerable - I wonder why, even in MVC3, you would use these custom collection classes that don't implement IEnumerable... it's off topic, but it does seem like a bad design decision...? – PandaWood Feb 04 '11 at 00:40
  • In this day and age with genetics, I would have to agree. – Lasse V. Karlsen Feb 04 '11 at 00:47
-6
[HttpPost]
public ActionResult Index(FormCollection collection)
{
     Dictionary<string,object> form = new Dictionary<string, object>();
     collection.CopyTo(form);
     return View();
}
smashraid
  • 1
  • 2