简体   繁体   中英

DataTables vs IEnumerable<T>

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 implement IEnumerable<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.

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.

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.

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.

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.

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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