Linq dynamically adding where conditions

前端 未结 3 395
一生所求
一生所求 2020-12-03 16:23

I have a gridview which has drop down boxes in each header for filtering. Each filter is loaded with the distinct values from its column when loaded. At run time, I add \"AL

3条回答
  •  清歌不尽
    2020-12-03 17:08

    You could create a helper method that handles the All logic. Something like:

    private bool CompareSelectedValue(string value, string dropDownValue)
    {
      if(dropDownValue == "ALL")
        return true;
      else
        return value == dropDownValue;
    }
    

    Then your query could be:

    var filteredList = (from x in allQuotes
                              where (CompareSelectedValue(x.SalesRepFullName, drpOwners.SelectedValue)                                    
                                    && CompareSelectedValue(x.CompanyName, drpCompanyName.SelectedValue)
                              select x);
    

提交回复
热议问题