views:

137

answers:

5

I am writing a class to save searches on my site. I want to have in the class an "Array" of all the parameters that were specified. I tried a NameValueCollection but the problem I ran into is when I have a multi-select (e.g. states) it only stores one of the entries because the key gets taken. I need a collection type that will let me have something like the following:

Name => Bob
State => Alaska
State => Oregon
State => Washington
Company => Acme

What type of collection should I use?

EDIT: ==============================

I'm not sure the comments so far will help. Let me explain a little further. This search class will be used to save the parameters for any search on my site. Different searches may or may not have the same parameters. When this classes save method is called the search will be dumped into a database. One record will be created in the Searches table and as many records an there are items in the collection will be created in the SearchesParameters table. The SearchesParamaters table has these columns (ID,searches_ID,key,value).

The database could care less if there are two parameters with a key of "State". In order to keep my class generic enough to use on all searches without having to be updated I want to have a collection/array that will let me have key/value pairs and also let me have multiple instances of the same key. Really I just want to be able to call searchObj.addParameter(KEY,VALUE); How the class handles that on the back end is mostly irrelevant so long as i can reliably get the correct keys paired up with the correct values.

Is a collection the way to go with this or should I be considering something like two arrays one storing the keys and one storing the values?

+1  A: 

A Dictionary that maps String to an List<string>. Something like Dictionary<string, List<string>>.

If an element isn't there in the Dictionary, create a new List for the Key and add to it. Otherwise, simply add the new Value to the existing List.

sri
This is overkill for Name and Company fields where there is only one.
OJ
+1  A: 

Create a class, and store that class in a collection.

class Search
{
  public string Name { get; set; }
  public List<string> State { get; set; }
  public string Company { get; set; }
}

Then you can have multiple states per search. Add instances of this to List and away you to.

OJ
Hrm, it seems I may have misread. Are each of those values separate searches? Or is this one single search?
OJ
Its one single search but lets say on search A I search for (name=bob,state=Oregon,state=washington). I searched for a name and two of states(multi-select). In the database its easy, three records in the FK table.Now on search B (possible a different form) I search for (State=Oregon, Company=Apex) This time two records in the FK table.Due to the indefinite number of parameters I would like a really generic collection that just has some key value combinations and the ability to have more than one item with the same key.
William
If you want loose typing, then I'd go for what others have already suggested, Dictionary<string, List<string>>.
OJ
+1  A: 

what about a generic list (System.Collections.Generic)?

e.g.,

string name;
List<string> states;
string company;

You can read up about generic lists here

Scozzard
+1  A: 

You should use a List<KeyValuePair<string, string>>

Joe Flateau
A: 

I would use Dictionary<string, HashSet<string>>.

gWiz