C# LINQ filtering with nested if statements
- by Tim Sumrall
I have a learning project where a data grid is filtered by 3 controls (a checkbox and 2 dropdowns)
I'm about to wrap up and move on to another project as it works well but I don't like the complexity of nesting IF statements to capture all the possible combinations of the 3 filters and was wondering if there is a better way. For example: Something that would allow for more filters to be added easily rather than walking through all the nests and adding another level of madness.
private void BuildQuery()
{
EntityQuery<MASTER_DOCKS> query = QDocksContext.GetMASTER_DOCKSQuery();
if (Tonnage.IsChecked.HasValue && Tonnage.IsChecked.Value)
{
if (null != FilterWaterWay.SelectedValue)
{
string WaterwaytoFilterBy = FilterWaterWay.SelectedValue.ToString();
if (!string.IsNullOrWhiteSpace(WaterwaytoFilterBy) && WaterwaytoFilterBy != "[Select WaterWay]")
{
if (null != FilterState.SelectedValue)
{
string StateToFilterBy = FilterState.SelectedValue.ToString();
if (null != FilterState.SelectedValue && !string.IsNullOrWhiteSpace(StateToFilterBy) && StateToFilterBy != "[Select State]")
{
if (!string.IsNullOrWhiteSpace(StateToFilterBy) && StateToFilterBy != "[Select State]")
{
query = query.Where(s => s.WTWY_NAME == WaterwaytoFilterBy && s.STATE == StateToFilterBy && (s.Tons != "0" && s.Tons != "")).OrderBy(s => s.WTWY_NAME);
MyQuery.Text = "Tonnage, WW and State";
}
}
if (StateToFilterBy == "[Select State]") //waterway but no state
{
query = query.Where(s => s.WTWY_NAME == WaterwaytoFilterBy && (s.Tons != "0" && s.Tons != "")).OrderBy(s => s.WTWY_NAME);
MyQuery.Text = "Tonnage, WW No State";
}
}
}
else
{
if (null != FilterState.SelectedValue)
{
string StateToFilterBy = FilterState.SelectedValue.ToString();
if (null != FilterState.SelectedValue && !string.IsNullOrWhiteSpace(StateToFilterBy) && StateToFilterBy != "[Select State]")
{
if (!string.IsNullOrWhiteSpace(StateToFilterBy) && StateToFilterBy != "[Select State]")
{
query = query.Where(s => s.STATE == StateToFilterBy && (s.Tons != "0" && s.Tons != "")).OrderBy(s => s.WTWY_NAME);
MyQuery.Text = "Tonnage State No WW";
}
}
else
{
query = query.Where(s => (s.Tons != "0" && s.Tons != ""));
MyQuery.Text = "Tonnage No State No WW";
}
}
}
}
}
else //no tonnage
{
if (null != FilterWaterWay.SelectedValue)
{
string WaterwaytoFilterBy = FilterWaterWay.SelectedValue.ToString();
if (!string.IsNullOrWhiteSpace(WaterwaytoFilterBy) && WaterwaytoFilterBy != "[Select WaterWay]")
{
if (null != FilterState.SelectedValue)
{
string StateToFilterBy = FilterState.SelectedValue.ToString();
if (null != FilterState.SelectedValue && !string.IsNullOrWhiteSpace(StateToFilterBy) && StateToFilterBy != "[Select State]")
{
if (!string.IsNullOrWhiteSpace(StateToFilterBy) && StateToFilterBy != "[Select State]")
{
query = query.Where(s => s.WTWY_NAME == WaterwaytoFilterBy && s.STATE == StateToFilterBy).OrderBy(s => s.WTWY_NAME);
MyQuery.Text = "No Tonnage, WW and State";
}
}
if (StateToFilterBy == "[Select State]") //waterway but no state
{
query = query.Where(s => s.WTWY_NAME == WaterwaytoFilterBy).OrderBy(s => s.WTWY_NAME);
MyQuery.Text = "No Tonnage, WW No State";
}
}
}
else
{
if (null != FilterState.SelectedValue)
{
string StateToFilterBy = FilterState.SelectedValue.ToString();
if (null != FilterState.SelectedValue && !string.IsNullOrWhiteSpace(StateToFilterBy) && StateToFilterBy != "[Select State]")
{
if (!string.IsNullOrWhiteSpace(StateToFilterBy) && StateToFilterBy != "[Select State]")
{
query = query.Where(s => s.STATE == StateToFilterBy).OrderBy(s => s.WTWY_NAME);
MyQuery.Text = "No Tonnage State No WW";
}
}
else
{
LoadAllData();
MyQuery.Text = "No Tonnage No State No WW";
}
}
}
}
}
LoadOperation<MASTER_DOCKS> loadOp = this.QDocksContext.Load(query);
DocksGrid.ItemsSource = loadOp.Entities;
}