views:

313

answers:

3

Is there a way to pull out keys from a NameValueCollection that pertain to a certain pattern/naming convention without having to iterate through every value in the collection?

+3  A: 

A name value collection is not designed to be particularly efficient at searching like that. Whatever method you use, it has to go through all items. You could use LINQ; something like:

col.Keys.OfType<string>().Where(s => s.StartsWith("SomeString"))
Mehrdad Afshari
+1  A: 

You might be able to do a combination of regex and linq magic, but at the bottom of all this, you will need to iterate over every value to check them. If you use linq, you won't need to write the iterations, but under the hood they will still be done.

If you really need to speed things more up than this, you need to looks at what kind of pattern / naming conventions you're into, and then make a searchable data structure to fit those needs.

cwap
A: 

Two comments:

1) Your question is too vague. There are a variety of data structures, like suffix and prefix tries, variants on red-black trees, etc which support efficient searching. A useful answer to your question depends on exactly the kind of pattern / naming convention you're searching for. Post some sample input and expected output.

2) There's no point implementing a complicated data structure unless you really need it. The first question you should ask yourself is whether you really need efficiency: For a collection containing < 50,000 items, I really doubt you'll see a perceptible difference in memory, cpu, or performance searching your keys with a fancy data structure than a straightforward linear search.

Juliet
I'm asking about NameValueCollection because I'm dealing specifically with the ASP.NET Request.Form which is a NameValueCollection.In terms of point 2) good point!
AJM