I have an ObservableCollection of about 1000 objects that needs to be filtered (searched) by the end user. The user must be able to search by name or employee id. The List Control consumes FilteredEmployees and Employees is loaded up with everything on page load.
I currently have it set up 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 handled before this method.
This doesn't smell very efficent. Should I use two methods for this, or is there a better way to handle filtering?
I'd like to keep Employees at an unchanged state so I can repopulate FilteredEmployees to the full list without hitting the DB again.