DataTables与IEnumerable< T> [英] DataTables vs IEnumerable<T>
问题描述
我正在与另一位与我合作的程序员进行辩论。
对于数据库返回类型,是否存在任何显着的内存使用或性能差异或其他问题这应该使某人避免使用DataSet和DataTable,并且有助于实现 IEnumerable
...或反之亦然的类型
我更喜欢返回的类型,它们实现 IEnumerable
( T [],IList
这些日子只是懒惰的唯一原因是使用DataTables?
数据表绝对是比列表要重得多,无论是内存要求还是处理时间花费在创建/填充它们。
使用DataReader比使用DataTables更快(尽管更详细)(假设您正在使用DataAdapter填充它们)。
说...
除非这在某个地方它真的很重要,否则你可能会很好,而且这两种方法都足够快,所以只要在每一种情况下更舒服一些就行。 (有时你想用小代码填满它们,有时你想用小代码读取它们)
我自己倾向于在绑定到数据表时使用DataTables一个GridView,或者当我需要多个结果集的同时处于活动状态。
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
I prefer returning types which implementIEnumerable<T>
(T[], IList<T>, or just IEnumerable<T>
) 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.
Is the only reason to use DataTables these day just lazyness?
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).
That said... Unless this is in some place where it really matters, 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.
这篇关于DataTables与IEnumerable< T>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!