I have a multi-select checkbox. Depending on which one is checked, I want to combine the results into a single query. Sort of like:
if (Checkbox1.Checked)
{
var query1 = from t in table1 ...
}
if (Checkbox2.Checked)
{
var query2 = from t in table2 ...
}
DataGridView1.DataSource = query1.Union(query2); // obviously doesnt
// work since query1 and query2 are not defined in this scope.
Any idea how to combine these selectively?
Assuming the queries are of the same type, you could define the queries outside of the conditional statements.
First, a helper method that creates an empty enumerable of the same type as the parameter:
Then, the new code:
This performs just fine because the queries aren’t actually executed until they’re enumerated over, as in the call to
ToList().