I am currently trying to optimize my application. AllProducts contains about 24k rows (entries).
List<tblMATProduct> AllProducts;
Basically, I would like to replace the following (ForEachAsync() added):
AllProducts = db.tblMATProduct.Include(s => s.tblGrade).Include(s => s.tblMATProductState).Include(s => s.tblFormat).Include(s => s.tblProduct).ThenInclude(s => s.tblBatch).OrderBy(s => s.MATProductID).ToList();
with something like:
AllProducts = db.tblMATProduct.Include(s => s.tblGrade).Include(s => s.tblMATProductState).Include(s => s.tblFormat).Include(s => s.tblProduct).ThenInclude(s => s.tblBatch).ForEachAsync(// Do operations here).OrderBy(s => s.MATProductID).ToList();
All the Includes are necessary as I need to get info from all those tables starting from the main table tblMATProduct.
This would help me to remove the following operations which are very costly because of the unnecessary foreach (Building list with LINQ and then loading it again to modify its objects).
Basically, all the following code is used to modify objects from my List Allproducts and I'd like to move it in the ForEachAsync().
private void SetProductsListInfo()
{
foreach (var product in AllProducts)
{
SetNumLotCount(product);
SetProductTMD(product);
SetLimitedQuantityTags(product);
SetNFPATags(product);
}
}
private void SetNumLotCount(tblMATProduct product)
{
{
if (product.tblProduct != null && product.tblProduct.tblBatch != null)
{
product.ProductNumLotCount = "No. Lot (" + product.tblProduct.tblBatch.Count + ")";
}
else if (product.tblProduct == null)
{
}
else
{
product.ProductNumLotCount = "No. Lot (" + 0 + ")";
}
}
}
private void SetProductTMD(tblMATProduct product)
{
product.ProductTMD = new tblProductTMD();
product.DangerList = new List<tblDanger>();
product.ProductTransportClassList = new List<tblProductTransportClass>();
if (product.tblProduct != null)
{
product.ProductTMD = product.tblProduct.tblProductTMD.FirstOrDefault();
if (product.ProductTMD != null) product.DangerList = product.ProductTMD.tblDanger.ToList();
if (product.ProductTMD != null) product.ProductTransportClassList = product.ProductTMD.tblProductTransportClass.ToList();
}
product.DangerTypeList = new List<tblDangerType>();
if (product.DangerList != null)
{
foreach (var danger in product.DangerList)
{
if (danger.tblDangerType != null && danger.tblDangerType.RulingSystemID == 2) product.DangerTypeList.Add(danger.tblDangerType);
}
foreach (var dangerType in product.DangerTypeList)
{
if (dangerType != null) dangerType.DangerIcon = dangerType.DangerTypeIcon;
}
}
product.TransportClassList = new List<tblTransportClass>();
if (product.ProductTransportClassList != null)
{
foreach (var productTransportClass in product.ProductTransportClassList)
{
if (productTransportClass.tblTransportClass != null) product.TransportClassList.Add(productTransportClass.tblTransportClass);
}
}
}
private void SetLimitedQuantityTags(tblMATProduct product)
{
if (product.ProductTMD != null && product.ProductTMD.ProductTMDLimitedQty != null && product.ProductTMD.ProductTMDLimitedQty > 0) product.ProductLimitedQuantity = product.ProductTMD.ProductTMDLimitedQty.ToString();
}
private void SetNFPATags(tblMATProduct product)
{
if (product.tblProduct != null && product.tblProduct.tblProductNFPA != null)
{
foreach (var productNFPA in product.tblProduct.tblProductNFPA)
{
if (productNFPA.NFPATypeID == 1) product.ProductInflammabilityLevel = productNFPA.tblNFPAValue.NFPAValueCode;
if (productNFPA.NFPATypeID == 2) product.ProductToxicityLevel = productNFPA.tblNFPAValue.NFPAValueCode;
if (productNFPA.NFPATypeID == 3) product.ProductReactivityLevel = productNFPA.tblNFPAValue.NFPAValueCode;
}
if (product.ProductInflammabilityLevel == null) product.ProductInflammabilityLevel = "0";
if (product.ProductToxicityLevel == null) product.ProductToxicityLevel = "0";
if (product.ProductReactivityLevel == null) product.ProductReactivityLevel = "0";
}
}
Note that I am also open to other suggestions than using ForEachAsync().
Thanks in advance!