System.Data.DataTable 的轻量级替换

发布于 2024-10-03 10:21:39 字数 144 浏览 0 评论 0原文

表格数据作为分隔数据存储在数据库中。我知道这并不理想,但这就是系统的设计方式。我需要提取数据并将其解析为某种结构。

在过去,我会使用 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吲‖鸣 2024-10-10 10:21:39

这是来自 Telerik 开发人员之一的轻量级数据表:

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

它支持 INotifyCollectionChanged 和 INotifyPropertyChanged 所以它非常适合数据绑定。

我不会说它不那么“肮脏”,但它是一种替代方案。

Here's 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 wouldn't say it's any less "dirty", but it is an alternative.

囚我心虐我身 2024-10-10 10:21:39

好吧,只需创建一个将数据库的所有列作为属性的类即可。然后只需实例化 ICollection 的实现(List、Hashset 等)并填充它(例如使用 LINQ)。

  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;
    }
  }

并执行以下操作:

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);
    }
}

如果您正在访问存储在数据库中的数据,您可能需要查看 LinqToSql 或 LinqToEntities。

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.

糖粟与秋泊 2024-10-10 10:21:39

好吧,使用我开发的一个库,它包括类型到类型的转换等等,以及用于列出的数据读取器
https://www.nuget.org/packages/Generic.LightDataTable/

Well use a library i developed it include type to type convert and many more, and datareader to list
https://www.nuget.org/packages/Generic.LightDataTable/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文