Home > .net 2.0, c#, Visual Studio 2005 > Easing the Frustration of Long Predicate (.FindAll)’s

Easing the Frustration of Long Predicate (.FindAll)’s

May 1, 2007

The quickest feedback from the consumers and tests of the framework I’m creating has been:

What is the syntax to use Find/FindAll again?  Why is it so confusing?

So, how can we make:

StudentRoster studentsInGrade9 =
    new
StudentRoster(roster.FindAll(delegate(StudentRecord x) { return x.Grade.Equals(“09”); }));

Into something that’s a bit cleaner?

Static Methods!

Under the class (StudentRoster in this example), create a new method of type Predicate<objectType>.

public static Predicate<StudentRecord> GetByGrade(string grade)
{
    
return (delegate(StudentRecord x) { return x.Grade.Equals(grade); });
}

Now, we can call:

StudentCollection studentsInGrade9 =
  
new StudentCollection(collection.FindAll(StudentCollection.GetByGrade(“09”)));

My only question at this point is if I need to create a method for EVERY property or if there’s a way to do something like.

GetBy(StudentRecord.Grade, “09”)

Anyone out there have any ideas?

Tags: , , ,

  1. No comments yet.
  1. May 7, 2007 at 8:09 am
Comments are closed.
%d bloggers like this: