1 回答
TA贡献1909条经验 获得超7个赞
您可以从循环中提取,作为字典,而不是在每次迭代时查询:vendorspaymentTermsallTaxSchemes
private void accessVendorGridData() {
var vendors = _vendorservice
.GetAllVendorAdd()
.GroupBy(item => item.Id)
.ToDictionary(chunk => chunk.Key, chunk => chunk.ToList());
var paymentTerms = _vendorservice
.GetAllPaymentTerms()
.GroupBy(item => item.Id)
.ToDictionary(chunk => chunk.Key, chunk => chunk.SingleOrDefault());
var allTaxSchemes = _vendorservice
.GetAllTaxScheme()
.GroupBy(item => item.Id)
.ToDictionary(chunk => chunk.Key, chunk => chunk.SingleOrDefault());
foreach (var item in getAllVendorList) {
var Addr = vendors.TryGetValue(item.Id, out var addrs)
? addrs
: new List<Vendor>(); //TODO: put the right type instead of Vendor
var paymentTerm = paymentTerms.TryGetValue(item.PaymentTermId, out var term)
? term
: null;
var taxscheme = allTaxSchemes.TryGetValue(item.PaymentTermId, out var scheme)
? scheme
: null;
}
}
您当前的代码有
O(|getAllVendorList| * (|vendors| + |paymentTerms| + |allTaxSchemes|))
时间复杂度,这个有
O(|getAllVendorList| + |vendors| + |paymentTerms| + |allTaxSchemes|)
_vendorservice.GetXXX()但是,如果是对服务、RDBMS 等的查询,那么它不是一个解决方案,并且您必须在每次迭代时调用它(因为数据可以更改)
- 1 回答
- 0 关注
- 92 浏览
添加回答
举报