1 回答

TA貢獻(xiàn)1909條經(jīng)驗(yàn) 獲得超7個(gè)贊
您可以從循環(huán)中提取,作為字典,而不是在每次迭代時(shí)查詢: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;
}
}
您當(dāng)前的代碼有
O(|getAllVendorList| * (|vendors| + |paymentTerms| + |allTaxSchemes|))
時(shí)間復(fù)雜度,這個(gè)有
O(|getAllVendorList| + |vendors| + |paymentTerms| + |allTaxSchemes|)
_vendorservice.GetXXX()但是,如果是對(duì)服務(wù)、RDBMS 等的查詢,那么它不是一個(gè)解決方案,并且您必須在每次迭代時(shí)調(diào)用它(因?yàn)閿?shù)據(jù)可以更改)
- 1 回答
- 0 關(guān)注
- 139 瀏覽
添加回答
舉報(bào)