简体   繁体   English

ASP.NET应该使用哪种类型的集合?

[英]ASP.NET which type of collection should I use?

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 (eg states) it only stores one of the entries because the key gets taken. 我尝试了一个NameValueCollection,但我遇到的问题是当我有一个多选(例如状态)时,它只存储其中一个条目,因为密钥被占用。 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. 当调用此类save方法时,搜索将被转储到数据库中。 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. 将在Searches表中创建一条记录,并且将在SearchesParameters表中创建集合中的项目。 The SearchesParamaters table has these columns (ID,searches_ID,key,value). SearchesParamaters表包含这些列(ID,searching_ID,key,value)。

The database could care less if there are two parameters with a key of "State". 如果存在两个具有“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); 真的我只想调用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? 是一个集合的方式,或者我应该考虑像存储键和存储值的两个数组?

A Dictionary that maps String to an List<string> . 将String映射到List<string> Dictionary。 Something like Dictionary<string, List<string>> . 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. 否则,只需将新值添加到现有列表中。

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. 将此实例添加到List并离开。

what about a generic list (System.Collections.Generic)? 通用列表(System.Collections.Generic)怎么样?

eg, 例如,

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

You can read up about generic lists here 您可以在此处阅读有关通用列表的信息

您应该使用List<KeyValuePair<string, string>>

我会使用Dictionary<string, HashSet<string>>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该使用哪个ASP.NET控件? - Which ASP.NET Control should I use? 我应该使用哪个asp.net验证控件? - Which asp.net validation control should I use? ASP.Net我应该使用哪个网格控件以及应该如何使用它? - ASP.Net which grid control should I use and how should I use it? asp.net:应使用哪个数据收集来返回表 - asp.net: which data collection should be used to return a table 我应该在asp.net中使用模拟吗? - should I use impersonation in asp.net? 所有类型的文件都应使用哪种内容类型在asp.net中下载文件? - Which content type should use for all types of files to download file in asp.net? 我应该为ASP.NET Framework 1.1实时维护使用哪个Visual Studio版本 - which visual studio version should i use for asp.net framework 1.1 live maintenance 我应该使用哪个控件来显示ASP.NET数据库中的帖子记录? - Which control should I use to show post records from my database in ASP.NET? 我应该为我的项目使用哪个ASP.NET登录系统? 会员,SimpleMembership或身份? - Which ASP.NET Login system should I use for my project? Membership, SimpleMembership or Identity? 我应该使用哪种ASP.NET缓存技术来缓存视图查询的结果? - Which ASP.NET cache technique should I use to cache the results of a view query?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM