Passing Parameter to a Predicate in .Net2.0
In this post, we will see how to pass parameter to a method representing Predicate.
Let's say, we have a collection of SprintBacklogItems and we want to filter all the SprintBacklogItem with Title start's with, let say "QA" or "Dev Task" depending on a input parameter. Now from the previous post http://adilakhter.wordpress.com/2008/01/11/using-predicate-actor-of-net20/ we know that , predicate only have 1 parameter of type T.

Then, how to pass a input parameter _HeaderToSearch in Predicate?
- To do that, we need to a new object called ListMatcher -
{
private string_HeaderToSearch;
publicListMatcher(
stringheaderToSearch) { _HeaderToSearch = headerToSearch; }
public boolPredicate(
SprintBacklogItemitem) {
returnitem.Title.StartsWith(_HeaderToSearch,
StringComparison.InvariantCultureIgnoreCase); } }
- Next , I initialized the ListMatcher object and use the HeaderToSearch to filter the items-
matcher =
new ListMatcher(
"QA");
this.FindAll(matcher.Predicate);
Done.:)
Comments ()