Rick D
Rick D

Reputation: 139

C# / LINQ - conditional LINQ query based on checkbox status

I'm trying to build a LINQ query that executes as values change, however I only want to bottom 4 statements relating to price and surface area to run on the condition that a certain checkbox on my Windows form is ticked. My code is below

 var userSearchQuery =
            from sale in saleData
            where checkedCities.Contains(sale.City)
            && checkedBedrooms.Contains(sale.Bedrooms)
            && checkedBathrooms.Contains(sale.Bathrooms)
            && checkedHouseTypes.Contains(sale.HouseType)
            && minPrice <= sale.Price
            && maxPrice >= sale.Price
            && minSurfaceArea <= sale.SurfaceArea
            && maxSurfaceArea >= sale.SurfaceArea
            select sale;

Can anyone help with the best way to do this please

Upvotes: 0

Views: 1308

Answers (3)

Robert
Robert

Reputation: 2523

What you could do is make the base query just as it is. So just remove last 4 conditions that you wish to dinamically add depending on some condition from UI. You will see that your query is of type IQueryable.

var userSearchQuery =
        from sale in saleData
        where checkedCities.Contains(sale.City)
        && checkedBedrooms.Contains(sale.Bedrooms)
        && checkedBathrooms.Contains(sale.Bathrooms)
        && checkedHouseTypes.Contains(sale.HouseType);

Do not select anything yet. Now add your condition depending on UI.

if(checkBox1.Checked)
    userSearchQuery = userSearchQuery.Where(s => minPrice <= s.Price);
if(checkBox2.Checked)
    userSearchQuery = userSearchQuery.Where(s => maxPrice => s.Price);
if(checkBox3.Checked)
    userSearchQuery = userSearchQuery.Where(s => minSurfaceArea => s.SurfaceArea);
if(checkBox4.Checked)
    userSearchQuery = userSearchQuery.Where(s => maxSurfaceArea => s.SurfaceArea);

Finally execute the query by calling ToList().

var results = userSearchQuery.Select(s => s).ToList();

Upvotes: 1

Keyur PATEL
Keyur PATEL

Reputation: 2329

You could use the fact that 'true' returns the result as follows:

var userSearchQuery =
        from sale in saleData
        where checkedCities.Contains(sale.City)
        && checkedBedrooms.Contains(sale.Bedrooms)
        && checkedBathrooms.Contains(sale.Bathrooms)
        && checkedHouseTypes.Contains(sale.HouseType)
        && (*some condition is checked*) ? (minPrice <= sale.Price && maxPrice >= sale.Price && minSurfaceArea <= sale.SurfaceArea && maxSurfaceArea >= sale.SurfaceArea) : true
        select sale;

I have tested the syntax, but not the execution so let me know if it doesn't work as expected.

For reading reference about the '?' operator:

?: Operator (C# Reference)

EDITED: As per apocalypse's comment, there is no need to check the condition multiple times.

Upvotes: 0

Michał Żołnieruk
Michał Żołnieruk

Reputation: 2105

You can stack the queries, so first create an IEnumerable for all cases and then add additional queries to previous IEnumerable only when your checkbox is checked.

var userSearchQuery = from sale in saleData
                      where checkedCities.Contains(sale.City)
                      && checkedBedrooms.Contains(sale.Bedrooms)
                      && checkedBathrooms.Contains(sale.Bathrooms)
                      && checkedHouseTypes.Contains(sale.HouseType)
                      select sale;

if (checkbox.IsChecked)
{
            userSearchQuery = from sale in userSearchQuery 
                                  where minPrice <= sale.Price
                                  && maxPrice >= sale.Price
                                  && minSurfaceArea <= sale.SurfaceArea
                                  && maxSurfaceArea >= sale.SurfaceArea
                                  select sale;
}

Upvotes: 0

Related Questions