The List
Using anonymous methods as an implementation of the System.Predicate can help me to achieve this without using a Static method as predicate for the List
In the example given below I have followed this approach to search the occurrence of a string in a List
List
_users.Add(new User("John Micheal", 23, "Bahamas"));
_users.Add(new User("Steve Hickman", 34, "Kensas"));
_users.Add(new User("Micheal Norman", 24, "New York"));
_users.Add(new User("John Ruf", 45, "New Jersey"));
_users.Add(new User("Alisha Menon", 34, "Bangalore"));
_users.Add(new User("John A", 56, "New York"));
_users.Add(new User("Amir Sinha", 34, "Bangalore"));
List
delegate(User _user)
{
return _user.Name.Contains("John");
});
The _nameFilteredUsers returned the following List of users
John Micheal 23 Bahamas
John Ruf 45 New Jersey
John A 56 New York
Similarly we can use the same approach to filter users based on age as
List
delegate(User _user)
{
return _user.Age > 30;
});
You can also create a method that filters the List of users and returns the filtered list based on the parameter passed.
private List
{
return _users.FindAll(delegate(User _user)
{
return _user.Name.Contains(_searchString);
});
}
And call the method like
List
No comments:
Post a Comment