What is the fastest way to filter a list of strings when making an Intellisense/Autocomplete list?
- by user559548
Hello everyone,
I'm writing an Intellisense/Autocomplete like the one you find in Visual Studio. It's all fine up until when the list contains probably 2000+ items.
I'm using a simple LINQ statement for doing the filtering:
var filterCollection = from s in listCollection
where s.FilterValue.IndexOf(currentWord,
StringComparison.OrdinalIgnoreCase) >= 0
orderby s.FilterValue
select s;
I then assign this collection to a WPF Listbox's ItemSource, and that's the end of it, works fine.
Noting that, the Listbox is also virtualised as well, so there will only be at most 7-8 visual elements in memory and in the visual tree.
However the caveat right now is that, when the user types extremely fast in the richtextbox, and on every key up I execute the filtering + binding, there's this semi-race condition, or out of sync filtering, like the first key stroke's filtering could still be doing it's filtering or binding work, while the fourth key stroke is also doing the same.
I know I could put in a delay before applying the filter, but I'm trying to achieve a seamless filtering much like the one in Visual Studio.
I'm not sure where my problem exactly lies, so I'm also attributing it to IndexOf's string operation, or perhaps my list of string's could be optimised in some kind of index, that could speed up searching.
Any suggestions of code samples are much welcomed.
Thanks.