Link to home
Start Free TrialLog in
Avatar of Dinesh Bali
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


        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());
                            }
                        }
                    }
                }

Open in new window


Please advise.
Avatar of it_saige
it_saige
Flag of United States of America image

Could you provide the relevant structure of the class/property definitions?

-saige-
Avatar of Bill Prew
Bill Prew

Making some assumptions about data structures, and couldn't easily check for typos here, but perhaps a bit simpler:

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());
            }
    }

Open in new window


»bp
Avatar of Dinesh Bali

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.

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))
                    //{
                    //}

                }

Open in new window


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; }

    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Many thanks for your help