简体   繁体   English

C#中的结构或数据集列表?

[英]List of Structs or Dataset in C#?

I have some data I want to work with. 我有一些我想要使用的数据。 Two string and two numbers and I have say 8000 rows of data. 两个字符串和两个数字,我说8000行数据。

Is a dataset the best option here to use, or could I use a struct and have a list of structs? 数据集是这里使用的最佳选择,还是我可以使用结构并拥有结构列表?

Would there be much performance difference between the list and the dataset? 列表和数据集之间会有很大的性能差异吗?

DataSet s and DataTable s are often more verbose and have some overhead to access, but usually are interoperable with whatever data-binding sort of stuff you're using. DataSetDataTable通常更加冗长,并且有一些访问开销,但通常可以与您正在使用的任何数据绑定类型的东西进行互操作。

If you have the choice (ie you're not hooked to some component that uses DataTable s) I'd strongly suggest using an appropriate strongly-typed collection, like a generic List , Dictionary , or SortedDictionary . 如果您有选择(即您没有使用某个使用DataTable的组件),我强烈建议您使用适当的强类型集合,如通用ListDictionarySortedDictionary I propose that the flexibility and transparency will benefit you in the long run, if there is a long run to your project. 我建议,如果您的项目有很长的路要走,那么从长远来看,灵活性和透明度将使您受益。

PS Two strings and two numbers is big enough that I doubt you'll see any benefit from making it a struct instead of a class. PS两个字符串和两个数字足够大,我怀疑你会看到使它成为struct而不是类的任何好处。 Of course, you should profile it for yourself, but that's my intuition. 当然,你应该为自己描绘一下,但那是我的直觉。 I agree with the post mentioning that you should be sure you understand the fundamental differences between the two if this is a case of premature optimization. 我同意帖子提到你应该确定你理解两者之间的根本区别,如果这是一个过早优化的情况。

Ok, I'm extrapolating based on limited data, but the fact that you are asking between a list of structs and a dataset implies to me that you're somewhat new to C# and have not been introduced to the fact that a struct is a ValueType and therefore lives on the stack. 好吧,我是根据有限的数据进行推断,但你在结构列表和数据集之间询问这一事实对我来说意味着你对C#有点新,并且没有被引入结构是一个结构的事实ValueType因此存在于堆栈中。 You have probably heard that a struct grants you "better performance" somewhere, and want to get the best performance you can for your list of 8000 items. 您可能已经听说过某个结构可以让您在某个地方获得“更好的性能”,并且希望获得8000个项目列表的最佳性能。

First, I believe that you are prematurely optimizing. 首先,我认为你过早地进行了优化。 Don't. 别。 Do whatever works within the scope of your program. 做任何在你的程序范围内工作的东西。 If you are building this list of 8000 items yourself programmatically, perhaps from an XML or flat file, I'd suggest you use a list of objects , as that will be the easiest for you to program against. 如果您以编程方式自己构建8000个项目列表,可能是从XML或平面文件构建,我建议您使用对象列表,因为这对您来说是最容易编程的。 Your post does not imply that you have a relationship between two tabular sets of data, so a DataSet would be unnecessary. 您的帖子并不意味着您在两个表格数据集之间存在关系,因此不需要DataSet。

That said, if you are receiving that list from the database somehow, and your current data layer is ADO.NET 2.0 (and therefore returns DataSets and DataTables), then I'd say use that. 也就是说,如果您以某种方式从数据库接收该列表,并且您当前的数据层是ADO.NET 2.0(因此返回DataSet和DataTables),那么我会说使用它。 If you are receiving the list from the database but your data layer is not yet defined, I would suggest you look into LINQ to SQL or Entity Framework . 如果您从数据库接收列表但尚未定义数据层,我建议您查看LINQ to SQLEntity Framework

Again, I caution you against prematurely optimizing, especially given that you don't appear to understand how structs work. 同样,我提醒你不要过早地进行优化,特别是考虑到你似乎不了解结构是如何工作的。 Again this is an assumption and I apologize in advance if I am wrong. 这是一个假设,如果我错了,我会事先道歉。

Depends on what "work with" means to you. 取决于“与...合作”对您意味着什么。 If I were displaying the data in a DataGridView or some other control such as that, I'd opt for the DataSet/DataTable route. 如果我在DataGridView或其他控件中显示数据,我会选择DataSet / DataTable路由。 Provides you with the ability do some easy filtering of the data. 为您提供轻松过滤数据的能力。

Again, the performance difference of the List versus the DataSet depends on what you are doing. 同样,List与DataSet的性能差异取决于您正在做什么。 Lists are great for iterating through the container and things of that nature. 列表非常适合迭代容器和那种性质的东西。 DataSets and DataTables provide for easy operations such as ReadXML() (assuming you've got your data in XML, of course) that will take your elements, create columns and fill in the rows for you. DataSets和DataTables提供了简单的操作,例如ReadXML()(假设您已经获得了XML数据),它将获取元素,创建列并为您填充行。 That's pretty convenient. 这很方便。

Unless you are running on pretty restrictive hardware or have a lot else going on by other threads/processes, 8000 items isn't really a terribly large amount of data. 除非你在相当严格的硬件上运行或者其他线程/进程有很多其他事情,否则8000项并不是非常大量的数据。 I'd choose my container based on what I planned to do with it. 我根据我打算用它做的事情选择我的容器。 If I were using databinding, I'd opt for the DataSet. 如果我使用数据绑定,我会选择DataSet。

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

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