Hussein
Hussein

Reputation: 991

Filtering a Collection using Linq based on Combo boxes Selections

i use the following code to filter a collection based on the user options which are captured via combo boxes and sent to view model or controller to implement a cascade filtering:

 IEnumerable<SubsystemDTO> _ssDTOs = _subsystemService
         .GetAllSubsystemsList()
          .Where(s => s.MS != null 
               && s.MS.Equals(_subsystemRptPanelViewModel.SelectedMS) 
               && _subsystemRptPanelViewModel.SelectedMS != "All")
          .Where(s => s.Flag != null 
               && s.Flag.Equals(_subsystemRptPanelViewModel.SelectedFlag) 
               && _subsystemRptPanelViewModel.SelectedFlag != "All")
          .Where(s => s.Scope != null 
               && s.Scope.Equals(_subsystemRptPanelViewModel.SelectedScope)
               && _subsystemRptPanelViewModel.SelectedScope != "All");

I have 3 combo boxes which collect the user options and they are applied to the collection in cascading manner as shown. The the data are fetched from a database which contain values equal to those passed by the combo boxes my questions are:

1.If the user choose not to filter by Flag for example, so he selects option All in that combo box how to apply that to the above linq query

2.Generally if the user wants to filter by a value in the combo box that doesn't has peer in the database how to do that (like All option in 1 or the invert of an option)

Note: i tried to add the statement [_subsystemRptPanelViewModel.SelectedMS != "All"] to exclude implementing the filter if all selection is selected by the user but when selecting All option in one combo box, the result is an empty query result.

Upvotes: 0

Views: 316

Answers (1)

stuartd
stuartd

Reputation: 73253

If the user choose not to filter by Flag for example, so he selects option All in that combo box how to apply that to the above linq query.

You want to return the values where either the value matches what the user selected OR the user has selected 'All' values: this first example follows the pattern of your query above, and only returns values where the SubsystemDTO values (MS, Flag, Scope) are not null.

  .Where(s => s.MS != null 
     && (_subsystemRptPanelViewModel.SelectedMS == "All" 
     || s.MS.Equals(_subsystemRptPanelViewModel.SelectedMS)))

  // etc

If you wanted to return all values regardless of whether the SubsystemDTO properties are populated, you would move the ALL check to the top:

  .Where(s => _subsystemRptPanelViewModel.SelectedMS == "All"
     || (s.MS != null && s.MS.Equals(_subsystemRptPanelViewModel.SelectedMS)))

  // etc

Edit:

To mix "Not' into this query you have to compare the result of the Equals comparison with whether or not the value 'Is' or "Is Not' the one specified by the user

If you had a view model property called IsMatch which was true when the user wanted to match the selected value, and false when the values which do not match the selected value:

 .Where(s => _subsystemRptPanelViewModel.SelectedMS == "All"
    || (s.MS != null && s.MS.Equals(_subsystemRptPanelViewModel.SelectedMS)
                            .Equals(_subsystemRptPanelViewModel.IsMatch))

// etc

Upvotes: 1

Related Questions