Filtering ObservableCollection by User Input

I have an ObservableCollection of about 1000 objects that need to be filtered (searched) by the end user. The user should be able to search by employee name or identifier. List management consumes FilteredEmployees and Employees loads with everything that loads on the page.

Currently, I have configured it as such:

public ObservableCollection<EmployeeServicesData> Employees { get; set; } public ObservableCollection<EmployeeServicesData> FilteredEmployees { get; set; } internal void FilterEmployee(string searchText, bool isByName) { if (searchText.Length > 0) { IEnumerabe<EmployeeServicesData> filter; if (isByName) filter = Employees.Where(x => x.Name.Length >= searchText.Length).Where(x => x.Name.Substring(0, searchText.Length) == searchText.ToUpper()); else filter = Employees.Where(x => x.EmployeeNumber.ToString().Length > searchText.Length).Where(x => x.EmployeeNumber.ToString().Substring(0, searchText.Length) == text); foreach (EmployeeServicesData employee in filter) FilteredEmployees.Add(employee); } } 

Sanitation is processed prior to this method.

It doesn't smell very good. Should I use two methods for this or is there a better way to handle filtering?

I would like to keep Employees unchanged so that I can re-populate FilteredEmployees into a complete list without hitting DB again.

+6
c # silverlight
source share
4 answers

It looks like you are trying to figure out if the searchText is in the Employee name or in the Employee number.

Instead, you can do this:

 x.Name.IndexOf(searchText, StringComparison.OrdinalIgnoreCase) >= 0 x.EmployeeNumber.ToString().IndexOf(searchText, StringComparison.OrdinalIgnoreCase) >= 0 

Or you can use StartsWith instead of IndexOf.

Edit: Another problem with List Controls with large amounts of data in them is that it takes a lot of time to render. Therefore, if you have it unfiltered when you start, and Silverlight or WCF or everything that should display all 1000 in the control, even if you do not see them all, it may take a little time. Silverlight 3 has user interface virtualization , which would probably be the best optimization you could do here.

+1
source share

I know this is an old post, but I used it to help me with the filtering aspect, and noticed that SlipFish creates an ObservableCollection, looping around the IEnumerable collection.

Because the ObservableCollection constructor accepts an IEnumerable collection, an ObservableCollection can be created as follows:

 FilteredEmployees = new ObservableCollection<EmployeeServicesData>(filter); 
+2
source share

Take a look at this entry for a filtered observable collection.

+1
source share
0
source share

All Articles