Easy replacement for System.Data.DataTable

There are tabular data that is stored in the database as delimited data. I don’t know, but this is how the system was developed. I need to pull out the data and analyze it in some kind of structure.

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

+3
source share
3 answers

Well, just create a class that has all database columns as properties. Then just create an instance of ICollection (List, Hashset, etc.) and populate 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 stored in a database, you can look at LinqToSql or LinqToEntities.

+3
source

Here is a lightweight DataTable from one of the Telerik developers:

http://blogs.telerik.com/vladimirenchev/posts/09-04-23/lightweight-datatable-for-your-silverlight-applications.aspx

It supports INotifyCollectionChanged and INotifyPropertyChanged, so it works well for data binding.

I would not say that it is less "dirty", but this is an alternative.

+3
source

Source: https://habr.com/ru/post/1776265/


All Articles