2010-10-29 89 views
1

我得到约等于49000的业务实体(表数据)的集合。我想一定值从这个集合使用添加方法和手动添加值的属性从一种类型的集合复制到另一种类型的最快方法c#

例如 //返回arounf 49000 VendorProduct收集这是指表中的数据VendorProduct复制到哪吒收集

List<VendorProduct> productList = GetMultiple(req); 

foreach (VendorProduct item in productList) 
      { 
       VendorSearchProductWrapper wrapperItem = new VendorSearchProductWrapper(); 
       IEnumerable<ClientProductPreference> prodPrefClient = item.ClientProductPreferences.Where(e => (e.VendorProductId.Equals(item.Id) 
        && (e.AccountId.Equals(SuiteUser.Current.AccountId)))); 
       if (prodPrefClient.Count() == 1) 
       { 
        foreach (ClientProductPreference it in prodPrefClient) 
        { 
         wrapperItem.ClientProdID = it.Id; 
         wrapperItem.ClientProductDescription = it.Description; 
         wrapperItem.MarkupPct = it.MarkUpPct; 
         wrapperItem.SalesPrice = it.SalesPrice; 
        } 
       } 
       wrapperItem.Code = item.Code; 
       wrapperItem.ListPrice = item.ListPrice; 
       wrapperItem.Id = item.Id; 
       wrapperItem.DiscountPct = ValueParser.SafeDecimal(item.Discount, null); 
       wrapperItem.Discountgroup = item.DiscountGroup.DiscountGroup; 
       wrapperItem.Description = item.Description; 
       wrapperItem.Unit = item.Unit; 
       wrapperItem.ClientName = item.Account.ClientName; 
       products.Add(wrapperItem); 
      } 

要复制所有这些49000条记录,它需要很多时间。实际上5分钟只有100-200条记录被添加到列表中。

我需要在大约1分钟内快速复制这些值。

在此先感谢

弗朗西斯P.

回答

0

这应该发生得更快,即使有代码,你有。你确定那里没有任何冗长的操作吗?例如。延迟加载,其他数据呼叫,...

即使是小的可以在49000迭代

0

我@Bertvan同意了很大的影响。迭代不应该花费太多时间(即使记录是49K)。

我会建议考虑以下几行(可能要创建的问题):

if (prodPrefClient.Count() == 1) 

不知道什么是你想在这里achive,但是这个调用遍历一个懒惰的集合。请考虑是否需要此检查。

wrapperItem.DiscountPct = ValueParser.SafeDecimal(item.Discount, null); 

Int/double比较器确实需要一些处理时间。您还应该通过删除这一行代码来检查操作所花费的总时间。

希望这会有所帮助。

1
IEnumerable<ClientProductPreference> prodPrefClient = item.ClientProductPreferences.Where(e  => (e.VendorProductId.Equals(item.Id) 
       && (e.AccountId.Equals(SuiteUser.Current.AccountId)))); 
      if (prodPrefClient.Count() == 1) 
      { 
       foreach (ClientProductPreference it in prodPrefClient) 
       { 

这段代码有这么多错误。

  1. 尝试检索该值为SingleOrDefault,然后检查NULL。你这样做的方式是通过相同的数据迭代TWICE。第一次得到计数,第二次迭代在foreach中(这也是无用的,因为你知道集合只有一个项目,并且为一个项目创建整个迭代器是疯狂的)

  2. 是否可以使用某种字典?

  3. 检查延迟加载。当你知道你需要这些数据(并且我可以看到你需要它们)时,你应该急切地加载它们以减少数据库调用的次数。

  4. 这样做的最佳方法是制作专用SQL(或LINQ,因为它非常简单),可以在数据库上完整执行。这将是最快的解决方案。

相关问题