Given tables as:
Table1
Table2
Given a UI allow user to make fency query on:
And as result datagridview with everyfields.
I want to build conditional query as if not "any" add this condition.
Considering that, simple LINQ query is not applicable:
Table2
.Where(x => x.stringfield1 == dropdwonlist1.SelectedValue)
.Where(x => x.stringfield2 == dropdwonlist2.SelectedValue)
.Where(x => x.stringfield3 == dropdwonlist3.SelectedValue)
(...)
There is Expression trees in documentation but that looks too much.
Is there simplest way to build my dynamic query ?
Expression trees look scarier than they are, but you are right, in your situation they are unnecessary: you could use a static condition that is smart enough to ignore dropdowns that have no selection. You can do it like this:
Table2
.Where(x => dropdwonlist1.SelectedValue == null || x.stringfield1 == dropdwonlist1.SelectedValue)
.Where(x => dropdwonlist2.SelectedValue == null || x.stringfield2 == dropdwonlist2.SelectedValue)
.Where(x => dropdwonlist3.SelectedValue == null || x.stringfield3 == dropdwonlist3.SelectedValue)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With