0

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

1
  • if saledata is a list (and not a linq2sql query that will still run on a db) you can do (for each line) && (cb.Checked) ? minPrice <= sale.Price : true Commented Oct 17, 2017 at 7:38

3 Answers 3

1

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();
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply, this syntax throws a number of errors when typing it out. If I put a semicolon at the end of the gourth statement, there is no select clause in the query, and in the UI part in your second group of statements, I get an error stating that sale can't be declared here because it is already declared in an enclosing local scope
Yeah, you need semicolons on each of those. My bad. As for the already declared variable just put something else instead of sale. As for the select, just make one at the end - after all ifs, and you should be good to go.
0

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

2 Comments

Could you please point out why and how this solves the OPs issue. While it may technically answer the question, just pasting some code is not a good answer.
Thanks for pointing this out, I went a bit to fast with an answer, now I added an explanation and fixed a small error in the query.
0

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.

5 Comments

I commented that because I think this is a dupe -_-
Why did you check condition 4 times?
You can also skip ? operator and just use the or syntax: (some condition is not checked OR other queries)
Thanks for the reply, this half works, but not entirely. If the checkboxes aren't checked, the search function doesn't work at all... Also, the search result doesn't respond to the checkboxes being checked event, once I have entered a number to search by and checked the relevant checkbox, I have to then check or uncheck a checkbox in one of the initial four conditions for the result to update (hope that make sense)
@RickD Then it seems the issue is where you're calling this method (or evaluating it, if lazy loading). Are you working with INotifyPropertyChanged and listening for the "propertychanged" event, or code-behind?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.