简体   繁体   English

我应该使用什么样的数据结构?

[英]What kind of data structure should I use?

I've got a mysql table that has entries with a unique ID, then 3 properties.我有一个 mysql 表,其中包含具有唯一 ID 的条目,然后是 3 个属性。

I'm displaying the IDs in a ListBox, and the other information goes on other parts of my page.我在 ListBox 中显示 ID,其他信息出现在我页面的其他部分。 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?如果是这样,使用键是 ID 并且值是对包含该行所有值的数组的引用的 Hashtable 是不是很糟糕?

I'm using .NET 4, and coding this in C#.我正在使用 .NET 4,并在 C# 中进行编码。

A model looks appropriate to represent a row: model 看起来适合表示一行:

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.IEnumerable<Foo>代表您的 SQL 表。 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.然后,您当然会有一个存储库,其中包含允许您获取单个 model 的方法(给定它的Id或其他标准)并获取所有模型。 Then bind this model to your GUI.然后将此 model 绑定到您的 GUI。

The best option is to create a business object class out of it and pass it around as a List.最好的选择是从中创建一个业务 object class 并将其作为列表传递。

You can also use the built-in DataTable class if you just need raw data in a small app.如果您只需要小应用程序中的原始数据,也可以使用内置的 DataTable class。

I would store them in a DataSet which will contain a DataTable.我会将它们存储在包含 DataTable 的 DataSet 中。 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更多信息: 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.我不喜欢您设置 Hashtable 的方式,因为值数组可能有点草率。 I would aim more for Hashtable, if you want to bind in that manner.如果您想以这种方式绑定,我会更多地针对 Hashtable。 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.但是,从技术上讲,您不必将 go 设置到此级别,因为您可以设置任何可以使用 IEnumerable 和绑定的 object 类型。 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.当您绑定到 object 上的一个属性,然后根据过滤器(LINQ to Entities?)显示时,您可以根据用户的过滤器重用数据。

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.一个可能的警告是注意您在 web 服务器端持有的数据的大小,特别是如果它是基于“每个会话”类型加载的,因为您最终可能会消耗大量 memory 来处理所有可能的用户选择。 Ouch!哎哟!

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

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