简体   繁体   中英

Finding the right way to store data

I had planned on using a Dictionary or SortedList for this but as the project evolved while planning it, I'm not sure what to do.

My site will load a bunch of results from a DB Query and I need to be able to store these results somewhere.

Here's how the results look like:

为此应使用什么类型的词典/列表?

So, here's what I am storing:

Result #1 (int) > Name > Value
                > Title > Value
                > Message > Value
                > Date > Value
                > Email > Value

Result #2 (int) > Name > Value
                > Title > Value
                > Message > Value
                > Date > Value
                > Email > Value

Result #3 (int) > Name > Value
                > Title > Value
                > Message > Value
                > Date > Value
                > Email > Value

Result #4 (int) > Name > Value
                > Title > Value
                > Message > Value
                > Date > Value
                > Email > Value

... and so on. My main concern is that most of the Dictionaries and Lists and Sorted/Linked lists cannot have dupliate keys or values. So I can't use them for this, because each Result will have all the same Key names, like Name, Title, Message, etc. Values may differ, though many of the values such as Name and Date and other ones will be dupes. So I'm kinda stuck with not knowing which kind of Dictionary/List to choose which allows duplicate keys/names and not knowing how to get that 'one to many' relationship thing going on so i can store these results properly.

Can someone please help me with what would be the best way to go about this?

The keys for each result look the same - which suggests you should just encapsulate those into a separate class (eg "MailMessage" or whatever this is meant to be). You'd then just have a Dictionary<int, MailMessage> - or even a list of those results, if the keys will really be zero or one as the lowest, increasing by one with each result.

You'd then potentially need to write some code to assemble that collection from the database results you receive, with appropriate error checking (eg unknown property names, repeated properties for the same message etc).

Of course, that's assuming the data you've given us is really representative.

Do you not just need to create a class like this?

public class Result
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Title{ get; set; }
    public string Message { get; set; }
    public DateTime Date { get; set; }
    public string Email { get; set; }
}

Then map your DB results to something like an IList<Result> , or an IDictionary<int, Result> keyed off the Id property?

It looks like you just need to generate (either with a tool like Entity Framework or by hand) some classes to store your objects.

Then you can easily maintain a collection of your objects.

You could also use the Repository pattern, google it.

Create a structure (because you want to store only values, no method required) and store them in Generic List.

Sample Code:

List list = new List();

list.Add(new MyStruct(10));
list.Add(new MyStruct(20));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM