Reputation: 21
I have a requirement in my wp7 application that I have to do partial match searching (multiple fields say firstname and lastname) on a list collection (say contact list) on every keypress on search text box and bind to listbox. I have written linq query to get the result on texchanged event. I am getting the result as expected, however it is not quick in response if I have more than 500 items in the collection.
I have posted the code below, I really appreciate if someone can help me out in tunning the performance issue.
private void TechChanged(object sender, TextChangedEventArgs e)
{
IList<Contacts> results = searchcontList
.Where(tb => tb.SearchText.Contains(textsearch.Text))
.ToList();
//"SearchText" is an attribute in contacts class which is concatination values of all the fields in contacts class
listcontact.ItemsSource = results;
}
Upvotes: 1
Views: 1461
Reputation: 70122
It is quite likely that you will not be able to improve performance of the search (i.e. the time taken to find the matching items and render them to the UI) - so you will nee to look at changing the way your application works to make it feel more responsive.
Do you really need to perform the search on every character entered into the TextBox
?
A common solution to your problem is a concept called 'throttling' where you only perform your search when the user pauses in their text entry for a while. You can do this quite easily using Reactive Extensions as follows:
Observable.FromEvent<TextChangedEventArgs>(searchTextBox, "TextChanged")
.Select(e => ((TextBox)e.Sender).Text)
.Where(text => text.Length > 2)
.Throttle(TimeSpan.FromMilliseconds(400))
.Subscribe(txt => // do your search here!);
The above code ensures that there are more than two characters before you start searching, and throttles to ensure that only one search is performed per 400 milliseconds. See the codeproject article I wrote for more details:
Exploring Reactive Extensions (Rx) through Twitter and Bing Maps Mashups
Upvotes: 3