我们最近一直在使用LINQ进行工作,主要是使用LINQ-to-Objects。不幸的是,有些查询可能会有点复杂,特别是当它们开始涉及多个序列的组合时。当你遇到像这样的查询时,很难准确地知道发生了什么:
IEnumerable<LongType> myCompanies = relevantBusiness.Children_Companies
.Select(ca => ca.PR_ContractItemId)
.Distinct()
.Select(id => new ContractedItem(id))
.Select(ci => ci.PR_ContractPcrId)
.Distinct()
.Select(id => new ContractedProdCompReg(id))
.Select(cpcr => cpcr.PR_CompanyId)
.Distinct();
var currentNewItems = myCompanies
.Where(currentCompanyId => !currentLic.Children_Appointments.Select(app => app.PR_CompanyId).Any(item => item == currentCompanyId))
.Select(currentId => new AppointmentStub(currentLic, currentId))
.Where(currentStub=>!existingItems.Any(existing=>existing.IsMatch(currentStub)));
Items = existingItems.Union(newItems).ToList();
即使在调试时,有时也很难确定是谁在干什么以及何时发生。除了在序列上恣意地调用 "ToList" 以便更轻松地检查事情外,是否有人有关于如何调试“复杂”的LINQ的好建议?