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() == - && GetSelectedComboBoxPersonLanguageText() != -)    {        var dataGridPersonQuery = _InitialDataGridPersonList            .Where(w => w.Language.Equals(GetSelectedComboBoxPersonLanguageText()));        foreach (var person in dataGridPersonQuery)        {            _DataGridPersonList.Add(person);        }    }    else if (GetSelectedComboBoxPersonStateText() != - && GetSelectedComboBoxPersonLanguageText() == -)    {        var dataGridPersonQuery = _InitialDataGridPersonList            .Where(w => (w.PersonState.Equals(GetSelectedComboBoxPersonStateText())));        foreach (var person in dataGridPersonQuery)        {            _DataGridPersonList.Add(person);        }    }    else if (GetSelectedComboBoxPersonStateText() == - && GetSelectedComboBoxPersonLanguageText() == -)    {        foreach (var person in _InitialDataGridPersonList)        {            _DataGridPersonList.Add(person);        }    }    else if (GetSelectedComboBoxPersonStateText() != - && GetSelectedComboBoxPersonLanguageText() != -)    {        var dataGridPersonQuery = _InitialDataGridPersonList            .Where(w => (w.PersonState.Equals(GetSelectedComboBoxPersonStateText()))                    && (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.
Filter a list based on some ComboBoxes
c#;wpf
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 && personLanguageSpecified)    {        AddPersons(_InitialDataGridPersonList                   .Where(w => w.Language.Equals(personLanguage)));    }    else if (personStateSpecified && !personLanguageSpecified)    {        AddPersons(_InitialDataGridPersonList                   .Where(w => w.PersonState.Equals(personState)));    }    else if (!personStateSpecified && !personLanguageSpecified)    {        AddPersons(_InitialDataGridPersonList);    }    else if (personStateSpecified && personLanguageSpecified)    {        AddPersons(_InitialDataGridPersonList                   .Where(w => w.PersonState.Equals(personState) &&                               w.Language.Equals(personLanguage)));    }}private void AddPersons(IEnumerable<Person> 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<PropertyType, Func<Person, object>> _propertySelectors =    new Dictionary<PropertyType, Func<Person, object>>    {        [PropertyType.State] = p => p.PersonState,        [PropertyType.Language] = p => p.Language    }private void PopulatePersons(){    if (_InitialDataGridPersonList != null)    {        var propertyValues = Enum.GetValues(typeof(PropertyType))                                 .Cast<PropertyType>()                                 .ToDictionary(t => t,                                               t => GetComboBoxText(t));        AddPersons(_InitialDataGridPersonList.Where(p =>        {            return propertyValues.All(v => v.Value == - ||                                           _propertySelectors[v.Key](p).Equals(v.Value)));        });    }}private void AddPersons(IEnumerable<Person> 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.