简体   繁体   English

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

[英]How can I get a generic list from a datatable with the lowest overhead?

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. 我有一个名为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. 我的SQL数据库中还有一个名为tblEmployees的表,其中有七个列的标签有些相似。 My goal is to "convert" the results from a query to a generic list of Employee objects. 我的目标是将查询的结果“转换”为Employee对象的通用列表。 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. 我有一个采用DataRow的构造函数(如图所示)来处理“ property = dr [“ column”]'内容。

Looking forward to your thoughts. 期待您的想法。

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. 据我所知,您的方法没有任何明显的性能瓶颈。

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

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? 通过不实例化Employee对象? You would then have to do without the Employee object which would probably mess with your entity modeling. 然后,您将不需要Employee对象,而这可能会干扰您的实体建模。

Briefly looking at the code, and not seeing how it is used, I would return a IEnumerator instead of a list. 简要地看一下代码,而不看其用法,我将返回IEnumerator而不是列表。 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). 然后,您可以使用yield return语句,而不会在列表中循环两次(一次填充,一次显示)。

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

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. 这种方法通常比命中数据库的任何方法快一个数量级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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