What kind of data structure should I use?
I've got a mysql table that has entries with a unique ID, then 3 properties.
I'm di开发者_开发知识库splaying the IDs in a ListBox, and the other information goes on other parts of my page. It gets re-used often enough that I don't want to do another query every time I need to reference it. My question is this: What kind of data structure should I use to hold the row data?
Is a 2-dimensional array the best option? If so, is it poor style to use a Hashtable with the key being the ID and the value being a reference to an array containing all the values for that row?
I'm using .NET 4, and coding this in C#.
A model looks appropriate to represent a row:
public class Foo
{
public string Id { get; set; }
public string Prop1 { get; set; }
public string Prop2 { get; set; }
public string Prop3 { get; set; }
}
and IEnumerable<Foo>
to represent your SQL table. You would then of course have a repository with methods allowing you to fetch a single model given it's Id
or other criteria and to fetch all models. Then bind this model to your GUI.
I would store them in a DataSet which will contain a DataTable. Multiple datatables are returned and stored in one dataset if the situation arises. More here: http://msdn.microsoft.com/en-us/library/ms978448.aspx#bdadotnetdata4_topic2c
I am not fond of the way you have the Hashtable set up, as an array of values is potentially a bit sloppy. I would aim more for Hashtable, if you want to bind in that manner. Tehcnically, however, you don't have to go to this level, as you can set up any object type that can use IEnumerable and bind. Example:
List<ObjectType> myList = new List<ObjectType>();
As you are binding to one property on the object and then displaying, according to a filter (LINQ to Entities?), you have the ability to reuse the data based on the user's filter.
One possible warning is watch the size of the data you are holding on the web server side, especially if this loaded on a "per session" type of basis, as you can end up consuming large amounts of memory to handle all possible user selections. Ouch!
The best option is to create a business object class out of it and pass it around as a List.
You can also use the built-in DataTable class if you just need raw data in a small app.
精彩评论