C# 如何为集合设计多个筛选器

C# 如何为集合设计多个筛选器,c#,wpf,algorithm,data-structures,data-binding,C#,Wpf,Algorithm,Data Structures,Data Binding,我的模型: public class Person { public string Name { get; set; } public bool Sex { get; set; } public string Country { get; set; } } 在我的WPF应用程序中,我想将一个列表绑定到DataGridControl,该视图还支持按性别和国家筛选 我当前的解决方案是创建一个List PersonList来存储所有数据,并创建另一个List PersonBi

我的模型:

public class Person
{
    public string Name { get; set; }
    public bool Sex { get; set; }
    public string Country { get; set; }
}
在我的WPF应用程序中,我想将一个列表绑定到DataGridControl,该视图还支持按性别和国家筛选

我当前的解决方案是创建一个List PersonList来存储所有数据,并创建另一个List PersonBindingList来存储UI中应该显示的数据。当用户检查过滤器并单击“确定”时,使用Linq从PersonList查询并将结果分配给PersonBindingList以更新UI

但这应该保留两个列表。另外,我不希望每次用户更改过滤条件时都加载数据,因为数据量非常大,加载速度非常慢。还有其他解决办法吗?还有一件事,性和国家可以自由结合


如果您想在客户端过滤数据,请多谢(尤其是)您想要的:

public class ViewModel
{
    // this is a property for filtering
    public bool Sex
    {
        get { ... }
        set
        {
            if (_sex != value) 
            {
                _sex = value;
                OnPropertyChanged();
                PersonListView.Refresh();
            }
        }
    }

    // this is a property for filtering too
    public string Country
    {
        // call PersonListView.Refresh in setter, as in Sex property setter
    }  

    // this is a property for binding to DataGrid
    public ICollectionView PersonListView
    {
        get
        {
            if (_personListView == null)
            {
                _personList = LoadPersons();
                _personListView = new ListCollectionView(_personList)
                {
                    Filter = p => ShouldViewPerson((Person)p);
                }
            }
            return _personListView;
        }
    }

    private bool ShouldViewPerson(Person p)
    {
        // implement filtering logic here;
        // e.g.:
        return p.Country.StartsWith(Country) && p.Sex == Sex;
    }

    private ListCollectionView _personListView;
    private List<Person> _personList;
}

嗨,丹尼斯,谢谢你的回复。在您的代码中,性别和国家是ViewModel中的两个属性,如果我的过滤器支持多选,我应该在哪里调用Refresh方法?例如,我想展示美国和英国的男性。
// this is a filter item
public class CountryFilterItem : INotifyPropertyChanged
{
    public string CountryName { ... }
    public bool IsChecked { ... }
}

public class ViewModel
{
    // this property is a replacement for "Country";
    // bind it to some ItemsControl is the filter view
    public IEnumerable<CountryFilterItem> Countries
    {
        get { return _countries; }
    }

    // fill filter somewhere;
    // when filling, subscribe on `PropertyChanged` event of each item;
    // when user will change IsChecked for item, you'll update filter:
    // 
    // var country = new Country { CountryName = "Zimbabwe" };
    // country.PropertyChanged += (sender, args) =>
    // {
    //     if (propertyName == "IsChecked")
    //     {
    //         PersonListView.Refresh();
    //     }
    // };
    // _countries.Add(country);
    private List<CountryFilterItem> _countries;

    // filtering logic
    private bool ShouldViewPerson(Person p)
    {
        // implement filtering logic here;
        // e.g.:
        return _countries.Any(_ => _.IsChecked && _.CountryName == p.Country) && p.Sex == Sex;
    }

    // other code here...
}