I'm filling a list of items from the database, which is taking extremly long time to run (Approx 60 seconds)
Here is the code with the relevant breakdown
97% of the performance issues come in the foreach loop at the bottom. Is there a better way to match list items on the nested object.
private List<ROPSetting> TestMethod()
{
using DataSet ds = DB.GetDs($"EXEC GetSupplyChainROP_C @DateFrom={DateTime.Now.AddYears(-1).ToDbQuote()}");
// load DS takes 3 seconds.
using var itemReader = ds.Tables[0].CreateDataReader();
var items = itemReader.ParseList<ROPSetting>().ToList();
using var usageReader = ds.Tables[1].CreateDataReader();
var usageItems = usageReader.ParseList<ItemUsage>().ToList();
// 97% of the performance impact is below. Is there a better way to match
foreach (var itm in items)
{
itm.Usage = usageItems
.Where(a => a.ItemCode == itm.ItemCode && a.WarehouseCode == itm.WarehouseCode).ToList();
}
return items;
}
public class ItemUsage
{
public DateTime Date { get; set; }
public decimal Quantity { get; set; }
internal string ItemCode { get; set; } // for matching
internal string WarehouseCode { get; set; } // for matching
}
public class ROPSetting
{
public string ItemCode { get; set; }
public string PartNumber { get; set; }
public decimal AverageCost { get; set; }
public string SupplierCode { get; set; }
public string SupplierName { get; set; }
public int TotalLeadTime { get; set; }
public int IntervalTime { get; set; }
public int ReorderPoint { get; set; }
public string WarehouseCode { get; set; }
public IList<ItemUsage> Usage { get; set; }
}
GetSupplyChainROP_C
procedure. You just want to join the two tables, and select the columns you want from both, then just call the procedure, and get its result. \$\endgroup\$