在用户键入时过滤大型集合(28000个成员) [英] Filtering a large collection (28000 members) as the user types
问题描述
我试图做的是在用户输入TextBox时过滤公司的ObservableCollection。用户可以检查CheckBoxes以便为他们想要搜索的内容添加更多标准。
很抱歉,如果这是一个特别长的例子,但正如我所说的,此方法目前非常缓慢,我正在寻找改善它的方法。
我的尝试:
我尝试过的主要问题当用户输入SearchBox时,它确实非常非常慢。以下是我首先应用过滤器的方法;
What I have attempted to do is filter an ObservableCollection of Companies as the user is typing into a TextBox. The user is able to check CheckBoxes to add further criteria to what they would like to search on.
Sorry if this is a particularly long example but as I said, the method is very slow at the moment and I am looking for ways to improve it.
What I have tried:
The main issue with what I have tried so far is that it is really,really slow when the user types into the SearchBox. Here is how I apply the filter first of all;
CollectionViewSource.GetDefaultView(Companies).Filter = FilterCompanies;
当用户输入TextBox时,会调用此方法;
When the user types into the TextBox this method is called;
private void SearchBoxCallApplyFilters(object sender, TextChangedEventArgs e)
{
CollectionViewSource.GetDefaultView(Companies).Refresh();
}
然后使用FilterCompanies方法;
And then the FilterCompanies method;
private bool FilterCompanies(object obj)
{
var company = obj as CompanyModel;
if (searchBox.Text == string.Empty)
{
return true;
}
else if (CharactersOnly(company.Name.ToLower()).Contains((CharactersOnly(searchBox.Text).ToLower()))
|| CharactersOnly(company.Town.ToLower()).Contains((CharactersOnly(searchBox.Text).ToLower()))
|| CharactersOnly(company.Postcode.ToLower()).Contains((CharactersOnly(searchBox.Text).ToLower())))
{
if (FilterByCheckBoxes(company))
{
return true;
}
}
return false;
}
最后是FilterByCheckBoxes方法;
And finally the FilterByCheckBoxes method;
private bool FilterByCheckBoxes(CompanyModel company)
{
if (currentCheckBox.IsChecked == true && company.CurrentStatus == 1)
{
return true;
}
if (subbieCheckBox.IsChecked == true && company.Subcontractor == 1)
{
return true;
}
if (supplierCheckBox.IsChecked == true && company.Supplier == 1)
{
return true;
}
if (planthireCheckBox.IsChecked == true && company.Planthire == 1)
{
return true;
}
if (architectCheckBox.IsChecked == true && company.Architect == 1)
{
return true;
}
if (qsCheckBox.IsChecked == true && company.QS == 1)
{
return true;
}
if (projectManagerCheckBox.IsChecked == true && company.ProjectManager == 1)
{
return true;
}
if (structEngCheckBox.IsChecked == true && company.StructEng == 1)
{
return true;
}
if (servEngCheckBox.IsChecked == true && company.ServiceEng == 1)
{
return true;
}
return false;
}
CharactersOnly只删除TextBox文本中不是字符的任何内容;
CharactersOnly simply removes anything that isn't a Character from the TextBox Text;
private string CharactersOnly(string input)
{
char[] arr = input.ToCharArray();
arr = Array.FindAll(arr, (c => (char.IsLetterOrDigit(c))));
return new string(arr);
}
推荐答案
尝试反应性扩展: Step by Step–使用Reactive Extensions过滤列表(Rx框架)| Bunjeeb'博客 [ ^ ]
Try Reactive Extensions : Step by Step – Filtering List using Reactive Extensions (Rx Framework) | Bunjeeb's Blog[^]
这篇关于在用户键入时过滤大型集合(28000个成员)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!