开发者

fastest way to copy from a collection of one type to another c#

开发者 https://www.devze.com 2023-01-22 18:00 出处:网络
I get a collection of business entities (table data) equal to around 49000 . I am trying to copy certain values from this collection to anothr collect开发者_JAVA百科ion using Add method and manually a

I get a collection of business entities (table data) equal to around 49000 . I am trying to copy certain values from this collection to anothr collect开发者_JAVA百科ion using Add method and manually adding values to its properties

For example //Returns arounf 49000 VendorProduct collection this refers to the data in the table 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);
            }

To copy all of these 49000 records it takes lot of time .Infact for 5 mins only 100-200 records get added to the List.

I need to copy these values faster say in about 1 Minute.

Thanks In Advance

Francis P.


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)
                {

There is so much wrong in this code.

  1. Try retrieving this value as SingleOrDefault and then checking for NULL. The way you are doing it, is iterating TWICE over same data. First to get count, second to iterate in foreach (that is useless too, because you know collection have only 1 item and creating whole one iterator for one item is crazy)

  2. Is it possible to use some kind of Dictionary?

  3. Check for lazy loading. When you know you will need the data (and I can see you need them), you should eager-load them to reduce ammount of database calls.

  4. Best way to do this is to make dedicated SQL (or LINQ, because its quite simple) query, that would execute completly on DB. This will be fastest solution possible.


This should be happening faster, even with code you have. Are you sure there aren't any lengthy operations in there? Eg. lazy loading, other data calls, ...

Even small ones can have a big impact on a 49000 iteration


I agree with @Bertvan. The iteration should not take that much time (even if the records are 49K).

I would suggest considering the following lines ( which could be creating problem):

if (prodPrefClient.Count() == 1)  

Not sure what are you trying to achive here, But this call is iterating a lazy collection. Please consider if you need this check.

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

Int/double comparers do eat some processing time. You should also check the total time taken by the operation by removing this line of code.

Hope this would help.

0

精彩评论

暂无评论...
验证码 换一张
取 消