I have a very large data set. In order to optimize query performance I'm making queries based on which filters a user selected.
using (_db)
{
if (string.IsNullOrEmpty(CompanyID))
{
if (string.IsNullOrEmpty(HealthplanCode))
{
foreach (string x in _db.BM_OPT_MASTER.Select(y => y.OPT).Distinct())
{
currentComboBox.Items.Add(x);
}
}
else
{
foreach (string x in _db.BM_OPT_MASTER.Where(y => y.HPCODE == HealthplanCode).Select(y => y.OPT).Distinct())
{
currentComboBox.Items.Add(x);
}
}
}
else
{
if (string.IsNullOrEmpty(HealthplanCode))
{
foreach (string x in _db.BM_OPT_MASTER.Where(y => y.COMPANY_ID == CompanyID).Select(y => y.OPT).Distinct())
{
currentComboBox.Items.Add(x);
}
}
else
{
foreach (string x in _db.BM_OPT_MASTER.Where(y => y.COMPANY_ID == CompanyID && y.HPCODE == HealthplanCode).Select(y => y.OPT).Distinct())
{
currentComboBox.Items.Add(x);
}
}
}
}
As you can see this can get pretty annoying as more and more filter options are added. Is there a way to refactor this code in such a manner that the query is still optimized without relying on nested if else statements?