由于执行时间很长,我该怎么做才能使这个 foreach 循环运行得更快?

我该怎么做才能让这个循环运行得更快?


    private void accessVendorGridData()

    {

        try

        {

            foreach (var item in getAllVendorList)

            {

                item.CurrencyName = "USD";


                // Fetch Addresses in Vendor Grid

                var Addr = _vendorservice.GetAllVendorAdd().Where(x => x.vendorId == item.Id).ToList();

                if (Addr.Count > 0)

                {

                    item.VendorAddressLine = String.Format("{0}, {1}, {2}, {3}, {4}", Addr[0].Address, Addr[0].City, Addr[0].StateProvince, Addr[0].ZipPostalCode, Addr[0].CountryRegion);

                }


                // Fetch Payment terms in Vendor Grid

                var paymentTerm = _vendorservice.GetAllPaymentTerms().Where(x => x.Id == item.PaymentTermId).ToList().SingleOrDefault();

                if (paymentTerm != null)

                {

                    item.paymenttermitem = paymentTerm.Name;

                }


                // Fetch Tax Scheme in Vendor Grid

                var taxscheme = _vendorservice.GetAllTaxScheme().Where(x => x.Id == item.TaxschemeId).ToList().SingleOrDefault();

                if (taxscheme != null)

                {

                    item.TaxschemeName = taxscheme.TaxSchemaName;

                }

            }

        }

        catch (Exception ex)

        {

            _exLog.AddErrorLog(ex, "NewVendor, accessVendorGridData()");

            ModernDialog.ShowMessage(ex.Message, "Error!", MessageBoxButton.OK);

        }

    }

我该怎么做才能让这个循环运行得更快?我尝试了 Parallel.ForEach 但在中间迷失了。有人可以帮忙吗?



倚天杖
浏览 94回答 1
1回答

jeck猫

您可以从循环中提取,作为字典,而不是在每次迭代时查询:vendorspaymentTermsallTaxSchemesprivate void accessVendorGridData() {&nbsp; var vendors = _vendorservice&nbsp; &nbsp; .GetAllVendorAdd()&nbsp;&nbsp;&nbsp; &nbsp; .GroupBy(item => item.Id)&nbsp; &nbsp; .ToDictionary(chunk => chunk.Key, chunk => chunk.ToList());&nbsp; var paymentTerms = _vendorservice&nbsp; &nbsp; .GetAllPaymentTerms()&nbsp; &nbsp; .GroupBy(item => item.Id)&nbsp; &nbsp; .ToDictionary(chunk => chunk.Key, chunk => chunk.SingleOrDefault());&nbsp; var allTaxSchemes = _vendorservice&nbsp; &nbsp; .GetAllTaxScheme()&nbsp; &nbsp; .GroupBy(item => item.Id)&nbsp; &nbsp; .ToDictionary(chunk => chunk.Key, chunk => chunk.SingleOrDefault());&nbsp; foreach (var item in getAllVendorList) {&nbsp; &nbsp; var Addr = vendors.TryGetValue(item.Id, out var addrs)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;? addrs&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;: new List<Vendor>(); //TODO: put the right type instead of Vendor&nbsp; &nbsp; var paymentTerm = paymentTerms.TryGetValue(item.PaymentTermId, out var term)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;? term&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;: null;&nbsp; &nbsp; var taxscheme = allTaxSchemes.TryGetValue(item.PaymentTermId, out var scheme)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;? scheme&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;: null;&nbsp; }&nbsp;}您当前的代码有O(|getAllVendorList| * (|vendors| + |paymentTerms| + |allTaxSchemes|))时间复杂度,这个有O(|getAllVendorList| + |vendors| + |paymentTerms| + |allTaxSchemes|)_vendorservice.GetXXX()但是,如果是对服务、RDBMS 等的查询,那么它不是一个解决方案,并且您必须在每次迭代时调用它(因为数据可以更改)
打开App,查看更多内容
随时随地看视频慕课网APP