我使用filehelpers libary将一些记录映射到csv文件,但它相当慢,所以我必须将它映射到另一个类。
private List<ItemsExport> MapItems(List<Items> tradeItem)
{
var retList = new List<ItemsExport>();
try
{
foreach (var item in Items)
{
retList.Add(new ItemsExport()
{
ItemCode = item.ItemCode,
BarCode = item.BarCode,
Description = item.Description,
SupplierCode = item.SupplierCode,
SupplierStockCode = item.SupplierStockCode,
Product_Group_Code = "",
Product_Group_Desc = "",
SportCode = GetProductAnaylisCodeValue(item.ItemCode, Constants.Sport).ToString(),
SportDesc = GetProductAnaylisCodeDesc(item.ItemCode, Constants.Sport).ToString(),
GenderCode = GetProductAnaylisCodeValue(item.ItemCode, Constants.Gender).ToString(),
GenderDesc = GetProductAnaylisCodeDesc(item.ItemCode, Constants.Gender).ToString(),
ColourCode = GetProductAnaylisCodeValue(item.ItemCode, Constants.Colour).ToString(),
ColourDesc = GetProductAnaylisCodeDesc(item.ItemCode, Constants.Colour).ToString(),
SizeCode = GetProductAnaylisCodeValue(item.ItemCode, Constants.Size).ToString(),
SizeDesc = GetProductAnaylisCodeDesc(item.ItemCode, Constants.Size).ToString(),
CategoryCode = GetProductAnaylisCodeValue(item.ItemCode, Constants.Category).ToString(),
CategoryDesc = GetProductAnaylisCodeDesc(item.ItemCode, Constants.Category).ToString(),
SearchCategoryCode = GetProductAnaylisCodeValue(item.ItemCode, Constants.SubCategory).ToString(),
SeearchCategoryDesc = GetProductAnaylisCodeDesc(item.ItemCode, Constants.SubCategory).ToString(),
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
});
}
}catch (Exception ex)
{
}
return retList;
}
以下是我如何调用上述内容。
using (var db = new xxxEntities1())
{
List<Items> items = db.Items.AsNoTracking().ToList();
var mapped = MapTradeItems(items);
}
有没有更好的方法转储到csv并不需要那么久?它唯一的报告项目是。
计数= 116378
这需要几分钟才能完成?这里的初始线只需几秒钟即可完成无跟踪的原因是我可以做些类似的事情来加速上面的工作吗?
列出项目= db.Items.AsNoTracking()。ToList();
我的GetProductAnaylisCodeValueValue位于代码完整性的下方。
public string GetProductAnaylisCodeValue(string ItemCode, string CategoryName)
{
string retResult = "";
try
{
using (var db = new xxxEntities1())
{
var paramItemCode = new SqlParameter("@ItemCode", ItemCode);
var paramCategoryName = new SqlParameter("@CategoryName", CategoryName);
StockItemSearchCategoryDescriptions_Result result = db.Database.SqlQuery<StockItemSearchCategoryDescriptions_Result>("StockItemSearchCategoryDescriptions @ItemCode ,@CategoryName", paramItemCode, paramCategoryName).FirstOrDefault();
retResult = result.ProductGroupValue;
}
}catch(Exception EX)
{
}
return retResult;
}
从下面的建议中确定我将我的呼叫改为内存列表,但每个内容仍然超级慢。
private List<ItemsExport> MapTradeItems(List<TradeItems> tradeItem)
{
var retList = new List<TradeItems>();
try
{
List<StandardLookUpList > _AnalsisCodes = GetAnayalsisCodesForExportCode();
foreach (var item in tradeItem)
{
retList.Add(new FuelImportSage.Classes.Export.TradeItemsExport()
{
ItemCode = item.ItemCode,
BarCode = item.BarCode,
Description = item.Description,
SupplierCode = item.SupplierCode,
SupplierStockCode = item.SupplierStockCode,
Product_Group_Code = "",
Product_Group_Desc = "",
SportCode = _AnalsisCodes.Where(w => w.ItemCode == item.ItemCode && w.code == Constants.Sport).FirstOrDefault().code.ToString(),
SportDesc = _AnalsisCodes.Where(w => w.ItemCode == item.ItemCode && w.code == Constants.Sport).FirstOrDefault().description.ToString(),
GenderCode = _AnalsisCodes.Where(w => w.ItemCode == item.ItemCode && w.code == Constants.Gender).FirstOrDefault().code.ToString(),
GenderDesc = _AnalsisCodes.Where(w => w.ItemCode == item.ItemCode && w.code == Constants.Gender).FirstOrDefault().description.ToString(),
ColourCode = _AnalsisCodes.Where(w => w.ItemCode == item.ItemCode && w.code == Constants.Colour).FirstOrDefault().code.ToString(),
ColourDesc = _AnalsisCodes.Where(w => w.ItemCode == item.ItemCode && w.code == Constants.Colour).FirstOrDefault().description.ToString(),
SizeCode = _AnalsisCodes.Where(w => w.ItemCode == item.ItemCode && w.code == Constants.Size).FirstOrDefault().code.ToString(),
SizeDesc = _AnalsisCodes.Where(w => w.ItemCode == item.ItemCode && w.code == Constants.Size).FirstOrDefault().description.ToString(),
CategoryCode = _AnalsisCodes.Where(w => w.ItemCode == item.ItemCode && w.code == Constants.Category).FirstOrDefault().code.ToString(),
CategoryDesc = _AnalsisCodes.Where(w => w.ItemCode == item.ItemCode && w.code == Constants.Category).FirstOrDefault().description.ToString(),
SearchCategoryCode = _AnalsisCodes.Where(w => w.ItemCode == item.ItemCode && w.code == Constants.SubCategory).FirstOrDefault().code.ToString(),
SeearchCategoryDesc = _AnalsisCodes.Where(w => w.ItemCode == item.ItemCode && w.code == Constants.SubCategory).FirstOrDefault().description.ToString(),
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
});
}
}catch (Exception ex)
{
}
return retList;
}