Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • MB 273 posts 936 karma points
    Dec 22, 2016 @ 13:19
    MB
    0

    Bool property show True & False items

    Hey guys,

    I'm trying to display products based on a boolean.

    But by default I'd like it to display products in both true and false.

    TLDR

    I declare my isMale to be true which by default ONLY displays all products which is true. This is not the point, I also wan't false products.

    I tried writing it like this:

    bool isMale = bool.Parse(queryString.Get("gender"));
    

    But that returns a error telling me the Value cannot be null. when no gender is selected.

    Live example here

    EXPLAINING THE CODE:

    First I check IF the queryString.Get("gender") isn't null. Great, it isn't null and there's a price-filter applied so now it renders out the products in the first selectedItems with the gender & price.

    Second In the else I say that if there's NO price filtering applied it should check AGAIN if the queryString.Get("gender") exist. If it does, it renders out all products but only those in the selected gender.

    Default with NO selected gender if there ISN'T any queryString.Get("gender") applied it looks in bool isMale in the top of the code and takes it default True value. How do I prevent this? How do I make the first bool isMale true AND false?

    ALL CODE: Prettier code snippet here

    @{
                        bool isMale = true;
                    }
                    @if (!string.IsNullOrEmpty(selectedpriceRange) && selectedpriceRange.Contains("-"))
                    {
                        string[] priceArray = selectedpriceRange.Split('-');
                        int minPrice = 0;
                        int maxPrice = 0;
    
                        if (queryString.Get("gender") != null)
                        {
                            isMale = bool.Parse(queryString.Get("gender"));
                        }
    
                        if (priceArray.Count() == 2 && int.TryParse(priceArray[0], out minPrice) && int.TryParse(priceArray[1], out maxPrice))
                        {
                            selectedItems.AddRange(productTypes
                                .Where(x => x.HasValue("price") &&
                                x.GetPropertyValue<int>("price") > minPrice &&
                                x.GetPropertyValue<int>("price") < maxPrice &&
                                x.HasValue("gender") &&
                                x.GetPropertyValue<bool>("gender") == isMale)
                                .Skip((page - 1) * pageSize).Take(pageSize));
    
                            foreach (var item in selectedItems.Skip((page - 1) * pageSize).Take(pageSize))
                            {
                                /* If pricerange is selected */
                                @buildItemProduct(item);
                            }
                        }
                    }
                    else
                    {
                        if (queryString.Get("gender") != null)
                        {
                            isMale = bool.Parse(queryString.Get("gender"));
                        }
                        selectedItems.AddRange(productTypes.Where(x => x.HasValue("gender") && x.GetPropertyValue<bool>("gender") == isMale));
    
                        foreach (var item in selectedItems.Skip((page - 1) * pageSize).Take(pageSize))
                        {
                            /* If pricerange is selected */
                            @buildItemProduct(item);
                        }
                    }
    
  • MB 273 posts 936 karma points
    Dec 23, 2016 @ 14:43
    MB
    100

    I asked the same question on stack overflow. I think I wrote my question a lot better there: See the thread here

    The solution to show both true and false products?

    Credit's to Thomas Ayoub

        var bothGender = true;
    if (queryString.Get("gender") != null)
    {
        isMale = bool.Parse(queryString.Get("gender"));
        bothGender = false;
    }
    
    if (priceArray.Count() == 2 && int.TryParse(priceArray[0], out minPrice) && int.TryParse(priceArray[1], out maxPrice))
    {
        selectedItems.AddRange(productTypes
            .Where(x => x.HasValue("price") &&
            x.GetPropertyValue<int>("price") > minPrice &&
            x.GetPropertyValue<int>("price") < maxPrice &&
            x.HasValue("gender") &&
            (bothGender || x.GetPropertyValue<bool>("gender") == isMale)) // <-- changes here
            .Skip((page - 1) * pageSize).Take(pageSize));
    
    //...
    
Please Sign in or register to post replies

Write your reply to:

Draft