C# 数据表与 IEnumerable<T>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/896238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 22:00:56  来源:igfitidea点击:

DataTables vs IEnumerable<T>

c#listdatatableienumerable

提问by CRice

I'm having a debate with another programmer I work with.

我正在与另一位与我一起工作的程序员进行辩论。

For a database return type, are there any significant memory usage or performance differences, or other cons which should make someone avoid using the DataSets and DataTables and favour types which implement IEnumerable<T>... or vice versa

对于数据库返回类型,是否有任何显着的内存使用或性能差异,或其他应避免使用 DataSets 和 DataTables 并支持实现的类型的缺点,IEnumerable<T>反之亦然

I prefer returning types which implementIEnumerable<T>(List<T>, T[] etc) because it's more lightweight, strongly typed to the object when accessing properties, allows richer information about the underlying type etc. They do take more time to set up though when manually using the data reader.

我更喜欢返回实现IEnumerable<T>( List<T>, T[] etc) 的类型,因为它更轻量级,在访问属性时对对象进行强类型化,允许有关底层类型等更丰富的信息。尽管手动使用数据读取器时,它们确实需要更多时间来设置。

Is the only reason to use DataTables these day just lazyness?

现在使用 DataTables 的唯一原因是懒惰吗?

采纳答案by Daniel Magliola

DataTables are definitely much heavier than Lists, both in memory requirements, and in processor time spent creating them / filling them up.
Using a DataReader is considerable faster (although more verbose) than using DataTables (I'm assuming you're using a DataAdapter to fill them).

数据表肯定比列表重得多,无论是在内存要求方面,还是在创建/填充它们所花费的处理器时间方面。
使用 DataReader 比使用 DataTables 快得多(虽然更冗长)(我假设您使用 DataAdapter 来填充它们)。

That said... Unless this is in some place where it reallymatters, you're probably fine either way, and both methods will be fast enough, so just go with whatever is more comfortable in each case. (Sometimes you want to fill them up with little code, sometimes you want to read them with little code)

也就是说......除非这是在某个真正重要的地方,否则你可能会很好,两种方法都足够快,所以在每种情况下都选择更舒适的方法。(有时你想用很少的代码填满它们,有时你想用很少的代码阅读它们)

I myself tend to only use DataTables when I'm binding to a GridView, or when I need more than one resultset active at the same time.

我自己倾向于只在绑定到 GridView 时使用 DataTables,或者当我需要同时激活多个结果集时。

回答by Jamie Penney

Using DataTables directly means tying yourself to the underlying data source and how it is laid out. This is not good from a maintainability point of view. If all your view needs is a list of some objects, that's all you should be giving it.

直接使用 DataTables 意味着将自己绑定到底层数据源及其布局方式。从可维护性的角度来看,这并不好。如果您的视图需要的只是一些对象的列表,那么您应该提供的就是这些。

回答by John M Gant

Another advantage to using the System.Collections classes is that you get better sorting and searching options. I don't know of any reasonable way to alter the way a DataTable sorts or searches; with the collection classes you just have your class implement IComparable or IEquatable and you can completely customize how List.Sort and List.Contains work.

使用 System.Collections 类的另一个优点是您可以获得更好的排序和搜索选项。我不知道有什么合理的方法可以改变 DataTable 的排序或搜索方式;使用集合类,您只需让您的类实现 IComparable 或 IEquatable,您就可以完全自定义 List.Sort 和 List.Contains 的工作方式。

Also with lists you don't have to worry about DBNull, which has tripped me up on more than one occasion because I was expecting null and got DBNull.

此外,对于列表,您不必担心 DBNull,它不止一次让我绊倒,因为我期待 null 并且得到了 DBNull。

回答by CRice

I also like the fact with IEnumerable<T>that you can enhance the underlying type of the collection with methods and properties which makes implementation far more elegant, and the code more maintainable. For example the FullName property. You can also add extension methods to the class if it is out of your control.

我也喜欢这样一个事实IEnumerable<T>,您可以使用方法和属性来增强集合的底层类型,这使得实现更加优雅,并且代码更易于维护。例如 FullName 属性。如果您无法控制,您还可以向类添加扩展方法。

public class SomeUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FullName { get { return String.Format("{0} {1}", FirstName, LastName); } }
}

回答by orion565

I found with large tables via sql, data table was much faster instead of IEnumerable. I dumped a table with 26k rows with 25 columns in a single HTML page. Datatable in 3 seconds, IEnumerable took 9 seconds. I vote DataTable. All codewad identical except the type.

我发现通过 sql 使用大表,数据表比 IEnumerable 快得多。我在单个 HTML 页面中转储了一个包含 26k 行和 25 列的表格。Datatable 用了 3 秒,IEnumerable 用了 9 秒。我投票给数据表。除类型外,所有代码都相同。