Return FormCollection elements with a prefix

How to return a subset of FormCollection with key / values ​​to the beat for those elements with a specific prefix? I can only do this for keys, but I need keys and values.

thanks

+3
source share
2 answers

Try ( verified ):

var form = Request.Form; var prefix = "prefix"; var asDictionary = form.Cast<string>() .Where(key => key.StartsWith(prefix)) .ToDictionary(key => key, key => form[key]) .ToList(); 
+6
source

Assuming the "form" is your FormCollection, I would try using Linq to do something like:

 FormCollection subset = form.Where(x => x.Key.Contains("YourPrefix_")); 

I have not tested this :)

Alternatively, you can change .Contains () to be .Substring (0.11) == "YourPrefix_", depending on how your keys are named, etc.

0
source

All Articles