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.
c # silverlight
Slipfish
source share