本文介绍了数据表与 IEnumerable 对比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在和另一位共事的程序员争论.

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

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

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

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

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.

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

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

推荐答案

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

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)

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

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.

这篇关于数据表与 IEnumerable 对比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 05:48