I’m sorry if this is a duplicate question, I have found some similar ones, but not one that would solve my problem. I have a collection of objects with various parameters and I want to filter them using data from ComboBoxes and TextBoxes.
var query = from zaj in zajezdy
where zaj.Zeme == (String)zemeCombo.SelectedValue
&& zaj.Oblast == (String)oblastCombo.SelectedValue
&& zaj.Stredisko == (String)strediskoCombo.SelectedValue
&& zaj.Doprava.Contains((String)dopravaCombo.SelectedValue)
&& zaj.Strava.Contains((String)stravaCombo.SelectedValue)
&& zaj.CenaOd > Int32.Parse(cenaOdText.Text)
&& zaj.CenaOd < Int32.Parse(cenaDoText.Text)
select zaj;
This code works if all the combos have been properly selected. However, if the user leaves some unselected/empty, the query comes back with zero objects in it. How can I detect which parameters are null so that the query ignores them?
I think this is a nice example for Specification usage.
Create object, which will represent
zajezdspecification:and initialize it with values from UI:
Use this specification to filter your collection:
PS try to use English names in your code.