Josh Josh - 8 months ago 100
C# Question

Lightweight Replacement for System.Data.DataTable

There is tabular data that is being stored in the database as a delimited data. Not ideal, I know, but that's how the system was designed. I need to pull the data out and parse it into some kind of structure.

In the old days, I'd use a DataTable and call it good. Now, however, that just feels dirty. What is a better, more efficient, and more lightweight structure to store tabular data in my data model?

Answer Source

Well, just create a class that has all the columns of the database as properties. Then just instantiate an implementation of ICollection (List, Hashset, etc..) and fill it (using LINQ for example).

  public class Customer
  {
    public int Id { get; set;}
    public string Name { get; set; }

    public Customer(int id, string name)
    {
        this.Id = id;
        this.Name = name;
    }
  }

And do something like:

List<Customer> customers = new List<Customer>();
using (DbDataReader reader = // instantiate reader)
{
    while (reader.Read())
    {
       Customer customer = new Customer(reader.GetInt32(0), reader.GetString(1));
       customers.Add(customer);
    }
}

If you are accessing data that is stored in a database you may want to look at LinqToSql or LinqToEntities.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download