2010-10-29 72 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,因爲它非常簡單),可以在數據庫上完整執行。這將是最快的解決方案。