我正在出口32,000种产品,但是有一个奇怪的问题,retList仅获得1917个项目,我不知道为什么会这样。
private List<FTradeItemsExport> MapTradeItems(List<TradeItems> tradeItem)
{
List<TradeItemsExport> retList = new List<TradeItemsExport>();
try
{
var StockImport = new Fuel_StockItemImports();
List<StandardLookUpList> _AnalsisCodes = GetAnayalsisCodesForExportCode();
StandardLookUpList sport = new StandardLookUpList();
StandardLookUpList gender = new StandardLookUpList();
StandardLookUpList colour = new StandardLookUpList();
StandardLookUpList Size = new StandardLookUpList();
StandardLookUpList categorycode = new StandardLookUpList();
StandardLookUpList categorydesc = new StandardLookUpList();
StandardLookUpList subcategorycode = new StandardLookUpList();
StandardLookUpList subcategorydesc = new StandardLookUpList();
StandardLookUpList brandcode = new StandardLookUpList();
StandardLookUpList branddesc = new StandardLookUpList();
foreach (var item in tradeItem)
{
var codesForThisItem = _AnalsisCodes.Where(w => w.ItemCode == item.ItemCode);
if (codesForThisItem.Any())
{
sport = codesForThisItem.FirstOrDefault(x => x.code == Constants.Sport);
gender = codesForThisItem.FirstOrDefault(x => x.code == Constants.Gender);
colour = codesForThisItem.FirstOrDefault(x => x.code == Constants.Colour);
Size = codesForThisItem.FirstOrDefault(x => x.code == Constants.Size);
categorycode = codesForThisItem.FirstOrDefault(x => x.code == Constants.Colour);
categorydesc = codesForThisItem.FirstOrDefault(x => x.code == Constants.Category);
subcategorycode = codesForThisItem.FirstOrDefault(x => x.code == Constants.Category);
subcategorydesc = codesForThisItem.FirstOrDefault(x => x.code == Constants.SubCategory);
brandcode = codesForThisItem.FirstOrDefault(x => x.code == Constants.Brand);
}
retList.Add(new TradeItemsExport()
{
ItemCode = item.ItemCode,
BarCode = item.BarCode,
Description = item.Description,
SupplierCode = item.SupplierCode,
SupplierStockCode = item.SupplierStockCode,
Product_Group_Code = "",
Product_Group_Desc = "",
SportCode = sport.LookupValue.ToStringOrEmpty(),
SportDesc = sport.description.ToStringOrEmpty(),
GenderCode = gender.LookupValue.ToStringOrEmpty(),
GenderDesc = gender.description.ToStringOrEmpty(),
ColourCode = colour.LookupValue.ToStringOrEmpty(),
ColourDesc = colour.description.ToStringOrEmpty(),
SizeCode = Size.LookupValue.ToStringOrEmpty(),
SizeDesc = Size.description.ToStringOrEmpty(),
CategoryCode = categorycode.LookupValue.ToStringOrEmpty(),
CategoryDesc = categorydesc.description.ToStringOrEmpty(),
subcategorycode = subcategorycode.LookupValue.ToStringOrEmpty(),
subcategorydesc = subcategorydesc.description.ToStringOrEmpty(),
BrandsCode = brandcode.LookupValue.ToStringOrEmpty(),
BrandsDesc = branddesc.description.ToStringOrEmpty(),
Vat = item.Vat,
GrossWeight = item.Weight,
CommodityCode = item.CommodityCode,
price_exVAT = item.price_exVAT,
price_incVAT = item.price_incVAT,
currentprice_exVAT = item.currentprice_exVAT,
currentprice_incVAT = item.currentprice_incVAT,
creation_date = item.creation_date,
Inactive_date = item.Inactive_date,
status = 1
});
var Stock = new Fuel_StockItemImport();
Stock.StockItem = item.ItemCode;
Stock.IsProcessed = true;
Stock.DateImported = DateTime.Now;
StockImport.Add(Stock);
StockImport.Update();
}
}
catch (Exception ex)
{
}
return retList;
}
如您所见,下面是映射列表中预期的项目的真实数字,我没有理解,因为我没有在数据上获得任何例外?当我单步执行代码并检查刹车点时,它没有用空引用轰炸到任何地方,因此无法确定发生了什么。
答案 0 :(得分:3)
发生这种情况的唯一方法是,在精确插入1912个项目之后,您将忽略循环中间的异常。
最可能的原因是在构造ToStringOrEmpty()
对象时调用TradeItemsExport
。您可以使用?.
运算符来解决此问题。
通常,您不应该忽略catch (Exception ex) { }
不执行任何操作的所有异常。如果您不知道如何处理异常,则不应有捕获该异常的代码。在这种情况下,这显然是编程问题(无法对对象进行null检查),因此应完全删除try
/ catch
。
(摘自评论)是否有更快的合并方法?
通过将Where(w => w.ItemCode == item.ItemCode)
替换为字典中基于哈希的查找,可以大大提高速度:
var byItemCode = _AnalsisCodes
.GroupBy(w => w.ItemCode)
.ToDictionary(g => g.Key, g => g.First());
...
foreach (var item in tradeItem) {
if (byItemCode.TryGetValue(item.ItemCode, out var codesForThisItem) && codesForThisItem.Any()) {
...
}
}
如果这样做不能充分加快速度,则需要在进入循环之前通过设置字典来重新组织codesForThisItem
内部的查找。