如何以最低的开销从数据表中获取通用列表?

发布于 2024-08-11 07:05:50 字数 625 浏览 2 评论 0原文

我在这里寻找最佳实践。对不起。我知道这是主观的,但是这里有很多聪明的人,所以应该有一些“非常好的”方法来做到这一点。

我有一个名为 Employee 的自定义对象。该对象有七个属性,例如姓名、电话、电子邮件等。我的 SQL 数据库中还有一个名为 tblEmployees 的表,其中有 7 列,其标记有些相似。我的目标是将查询结果“转换”为 Employee 对象的通用列表。做到这一点的最佳方法是什么(开销最低,最快)?

我目前正在做的事情是我在网络上看到的建议。我不喜欢它,因为我觉得它会减慢我的页面加载速度。通用列表让我的工作速度更快,但让我的客户付出代价让我感觉不太好。

这就是我正在做的:

List<Employee> list = new List<Employee>();
DataSet ds = Employee.searchEmployees("Byron");
foreach (DataRow dr in ds.Tables[0].Rows)
{
   list.Add(new Employee(dr));
}

我有一个构造函数,它采用一个 DataRow (如图所示)来处理 'property = dr["column"]' 的东西。

期待您的想法。

I'm looking for best practices here. Sorry. I know it's subjective, but there are a lot of smart people here, so there ought to be some "very good" ways of doing this.

I have a custom object called Employee. That object has seven properties like name, phone, email, and so on. There is also a table in my SQL database called tblEmployees with seven columns labeled somewhat similarly. My goal is to "convert" the results from a query to a generic list of Employee objects. What is the best way to do this (lowest overhead, quickest)?

What I am doing currently is something that I've seen proposed all over the web. I don't like it because I feel like it slows down my page loads. Generic lists make me faster at what I do, but I don't feel good about making my customers pay the price.

Here's what I'm doing:

List<Employee> list = new List<Employee>();
DataSet ds = Employee.searchEmployees("Byron");
foreach (DataRow dr in ds.Tables[0].Rows)
{
   list.Add(new Employee(dr));
}

I have a constructor that takes a DataRow (as shown) which handles the 'property = dr["column"]' stuff.

Looking forward to your thoughts.

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

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

发布评论

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

评论(5

栩栩如生 2024-08-18 07:05:50

您觉得这个过程的哪一部分比较慢?据我所知,您的方法没有任何明显的性能瓶颈。

Which part of the process do you feel is slow? Your methodology doesn't have any glaring performance bottlenecks as far as I can see.

遮了一弯 2024-08-18 07:05:50

简单地看一下代码,而不是看它是如何使用的,我会返回一个 IEnumerator 而不是列表。然后,您可以使用yield return 语句,并且不会循环遍历列表两次(一次用于填充,一次用于显示)。

所以...

protected IEnumerable<Employee> GetEmployees ()
{
   List<Employee> list = new List<Employee>();
   DataSet ds = Employee.searchEmployees("Byron");

   foreach (DataRow dr in ds.Tables[0].Rows)
   {
       yield return new Employee(dr);
   }
}

Briefly looking at the code, and not seeing how it is used, I would return a IEnumerator instead of a list. You can then use the yield return statement and you won't be looping through the list twice (one to populate and one to display).

so...

protected IEnumerable<Employee> GetEmployees ()
{
   List<Employee> list = new List<Employee>();
   DataSet ds = Employee.searchEmployees("Byron");

   foreach (DataRow dr in ds.Tables[0].Rows)
   {
       yield return new Employee(dr);
   }
}
欲拥i 2024-08-18 07:05:50

我建议使用像 Linq2SQL 或实体框架这样的 OR Mapper

I would suggest using an OR Mapper like Linq2SQL or entity framework

浪荡不羁 2024-08-18 07:05:50

构建列表的原因是为了在服务器端的函数之间传递。

与传递 DataSet 引用甚至 DataTable 相比,传递列表有什么优点?

抛开这些想法不谈,您当前正在做的是构建列表的标准程序。你认为怎样才能加快速度?通过不实例化 Employee 对象?然后,您将不得不放弃 Employee 对象,这可能会扰乱您的实体建模。

The reason for building a list is to pass between functions on the server side.

What is the advantage of passing the list as opposed to passing a DataSet reference or even a DataTable?

Those thoughts aside, what you are currently doing is standard procedure for building a list. How do you think you could speed it up? By not instancing the Employee object? You would then have to do without the Employee object which would probably mess with your entity modeling.

廻憶裏菂餘溫 2024-08-18 07:05:50

您现在正在做的事情需要在某个时刻以某种方式完成,并且您实际上无法比您所描述的方式更快地完成它。但是您可以通过缓存数据来减少必须执行此操作的次数。

将客户列表存储在缓存中。例如,在应用程序启动时填充它。当客户记录发生变化时,您可以更新缓存并将记录保存到数据库中。任何用户的任何读取都将进入缓存,而不是数据库。

这种方法通常比任何访问数据库的方法快一个数量级。

What you are doing now is something that needs to be done one way or another at some point, and you can't really do it significantly faster than the way you have described. But you can reduce the number of times you have to do it at all by caching your data.

Store the list of customers in the cache. Populate it on application start for example. When something changes a customer record you update the cache and save the record to the database. Any reads by any user will go to the cache, not the database.

This approach will usually be an order of magnitude faster than any approach that hits the database.

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