DataTables与IEnumerable< T> [英] DataTables vs IEnumerable<T>

查看:94
本文介绍了DataTables与IEnumerable< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



对于数据库返回类型,是否存在任何显着的内存使用或性能差异或其他问题这应该使某人避免使用DataSet和DataTable,并且有助于实现 IEnumerable ...或反之亦然的类型

我更喜欢返回的类型,它们实现 IEnumerable T [],IList 或只是IEnumerable< T& code>),因为它更轻便,访问属性时强烈地键入对象,允许有关底层类型的更丰富的信息等。尽管手动使用数据读取器时,它们需要更多的时间来设置。



这些日子只是懒惰的唯一原因是使用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&lt; T&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆