OK heres the catch I have. There is this our very famous employee class with some first name, last name and middle initial. I enter a value in a new Web combo control (Infragistics) well if you dont know what that is then think of a textbox control. All I am trying to do is to filter the list of employee objects based on the text entered and return the new list of filtered employees.
I have seen into predicates and using the delegates to mention the search criteria but the bad part is that my search criteria is not within my object.. I hate to send that search string as a part of my employee object.. and I would probably shout at people who suggest that..
So how do we mention our search criteria not within the object of the list which is being searched for..
My last alternative is using a enumerator to go through each of the list members and compare the search. It would be great if someone can point me to an article or method that can do this for me before I get into brute force. Please help before I write unhealthy code..
Thanks
Sri
AlanPosted Feb 13, 2008, 6:23 PM
Hi Sri,
Yes, you can have a list of lists of objects and the syntax is exactly as you'd surmised:
List
> list = new List
> ();
SriPosted Feb 13, 2008, 4:08 PM
Hey thanks! Thats appreciable effort. I did mention a search string variable in the class to solve this issue.
Thinking further on this. Can we have a list of list of objects?
Say company 1 has a list of employees and company 2 has a list of employees and so on.. then we have a combined list of list of employees of n companies.. The best employee is rated on a search criteria based on individual company policies and then the best among all companies is decided upon a different global search criteria applying to all companies.
It may not be fair to call them search criteria anymore.. Oh ya predicate now makes perfect sense to generalize the term search.. search is a form of predicate..
Looks like possible.. So, is a syntax of the form List
> possible??
Let me know
AlanPosted Feb 13, 2008, 2:52 PM
There's no need for the search criteria to be part of the Employee object but the predicate method does need to have access to the filter string as it can't be passed as a parameter. The filter string therefore needs to be available at class level which would be the case with a property of a control.
I've written this simple C# console app to demonstrate some of the power which predicates bring to .NET:
using System;
public class Program
{
static string letters;
public static void Main()
{
Console.Clear();
Console.Write("Enter first letter(s) of last name to filter by: ");
letters = Console.ReadLine();
Employee[] employees = new Employee[4];
employees[0] = new Employee("John", "D", "Brown");
employees[1] = new Employee("Fred", "P", "Bloggs");
employees[2] = new Employee("David", "E", "Smith");
employees[3] = new Employee("Colin","R", "Taylor");
Employee[] filteredEmployees;
filteredEmployees = Array.FindAll(employees, StartsWithLetters);
Console.WriteLine("\nThose employees whose last names begin with '" + letters + "' are:\n");
foreach(Employee employee in filteredEmployees)
{
Console.WriteLine(employee.FullName);
}
Console.ReadKey();
}
public static bool StartsWithLetters(Employee e)
{
return e.LastName.StartsWith(letters, StringComparison.InvariantCultureIgnoreCase);
}
}
public class Employee
{
string firstName, middleInitial, lastName;
public string FirstName
{
get{ return firstName; }
}
public string MiddleInitial
{
get{ return middleInitial; }
}
public string LastName
{
get{ return lastName; }
}
public string FullName
{
get{ return firstName + " " + middleInitial + " " + lastName; }
}
public Employee(string first, string middle, string last)
{
firstName = first;
middleInitial = middle;
lastName = last;
}
}