locked
MVC and Linq where with checkbox RRS feed

  • Question

  • User1901201124 posted

    Not sure if this belongs to the MVC forum as it may just be LINQ related. But if I have a search with a Checkbox (boolean type) IsClient ,  how do I exclude the where for IsClient if the checkbox is not check? 

    I can't do the below because the model.SearchIsCilent is a boolean type and there is no Null. But if it is not checked I want to return all results true or false.

    I could do something like "model.SearchIsClient == true ? x.Client==true : (x.Client==true || x.Client=false)" but there must be a better way? I have multiple different checkboxes.

    Hope that makes sense. Thanks

    model.Users = db.Users
    .Include(x => x.Address)
    .Where(x => (model.SearchIsClient == null || x.Client == true))
    Friday, October 4, 2019 10:33 PM

All replies

  • User1034446946 posted

    you could use a predicate builder

    http://www.albahari.com/nutshell/predicatebuilder.aspx

    Saturday, October 5, 2019 1:29 PM