Filter a list based on some ComboBoxes So I have a grid with data and above that grid 2 comboboxes, the grid will be filled with data based on the selected values from the comboboxes. This works but can you do it way easier like I did?



if (_InitialDataGridPersonList != null)
{
    if (GetSelectedComboBoxPersonStateText() == "-" &amp;&amp; GetSelectedComboBoxPersonLanguageText() != "-")
    {
        var dataGridPersonQuery = _InitialDataGridPersonList
            .Where(w =&gt; w.Language.Equals(GetSelectedComboBoxPersonLanguageText()));

        foreach (var person in dataGridPersonQuery)
        {
            _DataGridPersonList.Add(person);
        }
    }
    else if (GetSelectedComboBoxPersonStateText() != "-" &amp;&amp; GetSelectedComboBoxPersonLanguageText() == "-")
    {
        var dataGridPersonQuery = _InitialDataGridPersonList
            .Where(w =&gt; (w.PersonState.Equals(GetSelectedComboBoxPersonStateText())));

        foreach (var person in dataGridPersonQuery)
        {
            _DataGridPersonList.Add(person);
        }
    }
    else if (GetSelectedComboBoxPersonStateText() == "-" &amp;&amp; GetSelectedComboBoxPersonLanguageText() == "-")
    {
        foreach (var person in _InitialDataGridPersonList)
        {
            _DataGridPersonList.Add(person);
        }
    }
    else if (GetSelectedComboBoxPersonStateText() != "-" &amp;&amp; GetSelectedComboBoxPersonLanguageText() != "-")
    {
        var dataGridPersonQuery = _InitialDataGridPersonList
            .Where(w =&gt; (w.PersonState.Equals(GetSelectedComboBoxPersonStateText()))
                    &amp;&amp; (w.Language.Equals(GetSelectedComboBoxPersonLanguageText())));

        foreach (var person in dataGridPersonQuery)
        {
            _DataGridPersonList.Add(person);
        }
    }


Imagine there are like 10 comboboxes. So what I'm asking is, if there is a easier way with less code?

PS. I'm using MVVM so no code behind.
You have a lot of code duplication. As a first step, your code should be rewritten somehow like that:

if (_InitialDataGridPersonList != null)
{
    var personState = GetSelectedComboBoxPersonStateText();
    var personLanguage = GetSelectedComboBoxPersonLanguageText();

    var personStateSpecified = personState != "-";
    var personLanguageSpecified = personLanguage != "-";

    if (!personStateSpecified &amp;&amp; personLanguageSpecified)
    {
        AddPersons(_InitialDataGridPersonList
                   .Where(w =&gt; w.Language.Equals(personLanguage)));
    }
    else if (personStateSpecified &amp;&amp; !personLanguageSpecified)
    {
        AddPersons(_InitialDataGridPersonList
                   .Where(w =&gt; w.PersonState.Equals(personState)));
    }
    else if (!personStateSpecified &amp;&amp; !personLanguageSpecified)
    {
        AddPersons(_InitialDataGridPersonList);
    }
    else if (personStateSpecified &amp;&amp; personLanguageSpecified)
    {
        AddPersons(_InitialDataGridPersonList
                   .Where(w =&gt; w.PersonState.Equals(personState) &amp;&amp;
                               w.Language.Equals(personLanguage)));
    }
}

private void AddPersons(IEnumerable&lt;Person&gt; persons)
{
    foreach (var person in persons)
    {
        _DataGridPersonList.Add(person);
    }
}


As a second step, you can place all comboboxes in a map where you'll link each combobox with property selector:

public enum PropertyType
{
    State,
    Language
}

private static readonly Dictionary&lt;PropertyType, Func&lt;Person, object&gt;&gt; _propertySelectors =
    new Dictionary&lt;PropertyType, Func&lt;Person, object&gt;&gt;
    {
        [PropertyType.State] = p =&gt; p.PersonState,
        [PropertyType.Language] = p =&gt; p.Language
    }

private void PopulatePersons()
{
    if (_InitialDataGridPersonList != null)
    {
        var propertyValues = Enum.GetValues(typeof(PropertyType))
                                 .Cast&lt;PropertyType&gt;()
                                 .ToDictionary(t =&gt; t,
                                               t =&gt; GetComboBoxText(t));

        AddPersons(_InitialDataGridPersonList.Where(p =&gt;
        {
            return propertyValues.All(v =&gt; v.Value == "-" ||
                                           _propertySelectors[v.Key](p).Equals(v.Value)));
        });
    }
}

private void AddPersons(IEnumerable&lt;Person&gt; persons)
{
    foreach (var person in persons)
    {
        _DataGridPersonList.Add(person);
    }
}

private string GetComboBoxText(PropertyType propertyType)
{
    switch (propertyType)
    {
        case PropertyType.State:
            return GetSelectedComboBoxPersonStateText();
        case PropertyType.Language:
            return GetSelectedComboBoxPersonLanguageText();
        ...
    }

    return "-";
}


This approach allows you to add another comboboxes without a pain.