Dinesh Bali
asked on
Refactor Linq query
Hi,
I am working on C# and used Linq.
How can I factor my below code? It has too many foreach
Please advise.
I am working on C# and used Linq.
How can I factor my below code? It has too many foreach
foreach (var category in Model.ArrivalBooking.AvailableProducts.Where(t => t.Products.Any(x => ((x.IsVisible == true) && (x.ProductType == "C")))).OrderBy(b => b.SortOrder))
{
if (category.CategoryCode == "MNG" || category.CategoryCode == "TRN")
{
foreach (var product in category.Products.Where(t => t.ProductType == "C" && t.IsVisible == true).OrderBy(x => x.Rate))
{
if (coreServicesRules.ProductCodeList.Contains(product.ProductCode))
{
productDetailsInGrid = productDetailsInGrid.Replace("@@PRODUCTCODE@@", product.ProductCode);
productDetailsInGrid = productDetailsInGrid.Replace("@@PRODUCTCODEAEDRATE@@", product.Rate.ToString());
}
}
}
}
Please advise.
Making some assumptions about data structures, and couldn't easily check for typos here, but perhaps a bit simpler:
»bp
foreach (var category in Model.ArrivalBooking.AvailableProducts.Where(t => t.Products.Any(x => ((x.IsVisible == true) && (x.ProductType == "C")))).Where(x => (x.CategoryCode == "MNG" || x.CategoryCode == "TRN")).OrderBy(b => b.SortOrder))
{
foreach (var product in category.Products.Where(t => t.ProductType == "C" && t.IsVisible == true).Where(x => coreServicesRules.ProductCodeList.Contains(x.ProductCode)).OrderBy(x => x.Rate))
{
productDetailsInGrid = productDetailsInGrid.Replace("@@PRODUCTCODE@@", product.ProductCode);
productDetailsInGrid = productDetailsInGrid.Replace("@@PRODUCTCODEAEDRATE@@", product.Rate.ToString());
}
}
»bp
ASKER
Many thanks for your help.
Please find below structure. Basically I need list of
List<ProductItem> Products in foreach where product is visible and Type C.
I tried with to place in first foreach. But it is giving error. Then I tried to put in another foreach loop inside first for each.
I got the error:
productCategoryBE.Products .Any(q => ((q.IsVisible == true)))
error CS1061: 'List<ProductItem>' does not contain a definition for 'Any' and no extension method 'Any' accepting a first argument of type 'List<ProductItem>' could be found (are you missing a using directive or an assembly reference?)
I commented these lines.
Please advise.
Please find below structure. Basically I need list of
List<ProductItem> Products in foreach where product is visible and Type C.
I tried with to place in first foreach. But it is giving error. Then I tried to put in another foreach loop inside first for each.
I got the error:
productCategoryBE.Products
error CS1061: 'List<ProductItem>' does not contain a definition for 'Any' and no extension method 'Any' accepting a first argument of type 'List<ProductItem>' could be found (are you missing a using directive or an assembly reference?)
I commented these lines.
Please advise.
foreach (var category in Model.ArrivalBooking.AvailableProducts.Where(t => t.Products.Any(x => ((x.IsVisible == true) && (x.ProductType == "C")))).Where(x => (x.CategoryCode == "MNG" || x.CategoryCode == "TRN")).OrderBy(b => b.SortOrder))
{
ProductCategoryBE productCategoryBE = category;
//foreach (var category in productCategoryBE.Products.Any(q => ((q.IsVisible == true) && (q.ProductType == "C"))).OrderBy(x => x.Rate))
//{
//}
}
public class ArrivalBooking
{
public List<ProductCategoryBE> AvailableProducts { get; set; }
}
public class ProductCategoryBE
{
public string CategoryCode { get; set; }
public string CategoryName { get; set; }
public List<ProductItem> Products { get; set; }
public int SortOrder { get; set; }
}
public class ProductItem
{
public string ProductCode { get; set; }
public string ProductName { get; set; }
public double Rate { get; set; }
public bool IsVisible { get; set; }
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Many thanks for your help
-saige-