我找不到这个具体的例子,所以我发布了这个问题.任何帮助赞赏.
我有两个大的通用列表,都有超过300K的项目.
我循环遍历第一个列表以回读信息并为动态生成新列表的新项目,但是我需要在第二个列表中搜索并根据三个匹配条件返回一个值,如果发现要添加到列表,但是你可以想象,这样做300k * 300k次需要时间.
有什么方法可以更有效地做到这一点吗?
我的代码:
var reportList = new List<StocksHeldInCustody>(); foreach (var correctDepotHolding in correctDepotHoldings) { var reportLine = new StocksHeldInCustody(); reportLine.ClientNo = correctDepotHolding.ClientNo; reportLine.Value = correctDepotHolding.ValueOfStock; reportLine.Depot = correctDepotHolding.Depot; reportLine.SEDOL = correctDepotHolding.StockCode; reportLine.Units = correctDepotHolding.QuantityHeld; reportLine.Custodian = "Unknown"; reportLine.StockName = correctDepotHolding.StockR1.Trim() + " " + correctDepotHolding.StockR2.Trim(); //Get custodian info foreach (var ccHolding in ccHoldList) { if (correctDepotHolding.ClientNo != ccHolding.ClientNo) continue; if (correctDepotHolding.Depot != ccHolding.Depot) continue; if (correctDepotHolding.StockCode != ccHolding.StockCode) continue; if (correctDepotHolding.QuantityHeld != ccHolding.QuantityHeld) continue; reportLine.Custodian = ccHolding.Custodian; break; } reportList.Add(reportLine); }@H_301_12@
解决方法
正如Pranay所说,加入可能是你想要的:
var query = from correct in correctDepotHoldings join ccHolding in ccHoldList on new { correct.ClientNo,correct.Depot,correct.StockCode,correct.QuantityHeld } equals new { ccHolding.ClientNo,ccHolding.Depot,ccHolding.StockCode,ccHolding.QuantityHeld } // TODO: Fill in the properties here based on correct and ccHolding select new StocksHeldInCustody { ... }; var reportList = query.ToList();@H_301_12@ @H_301_12@