Search code examples
c#.netumbracoumbraco7

Bool property show True & False items


I'm working on this website in Umbraco 7. My problem is the "gender" filtering on the left side which is a bool (queryString). This bool is set on each product as a "Is this product for male? - True or False".

  • All gender = nothing
  • Male = &gender=true
  • Female = &gender=false

By default it's set to "All gender", but in the code, it's actually set to true as you can see in my code. I would like to show both true and false products at the same time.

The bool isMale = true should be something like isMale = true && false if that would make sense.

<div id="categoryCollection">

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

If you'd like to see my entire code you can see it here.

I don't know how to declare the "isMale" to show both true and false products at the same time.


Solution

  • You can use another bool to keep the change minimal:

    //...
    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));
    
    //...
    

    Or use an enumeration.