Predicate<T> matches me

Posted by Jonas Elfström Thu, 29 Jan 2009 20:29:00 GMT

I really like the Predicate(T) delegates that were added to the generic collections and lists in .NET 2.0. With the later addition of lambda expressions came cleanliness and readability.

Today we faced a quite simple problem that were made even simpler by the dear predicates. We had a kind of event log and wanted to filter it client side (Windows Forms) using a list of criterias. We began by implementing to filter by a number of categories. It ended up being only one row (in Visual Studio, for obvious reasons not here):

1
2
3
4
5
6
private List<Events> FilterEventsByCategory(List<Events> events,
                                        List<Category> categories) 
{
  return events.FindAll(event => 
      categories.Exists(category => category.CategoryId==event.CategoryId)); 
}


Neat!

Posted in C# | 1 comment

Comments

    1. Avatar
      jonelf Fri, 30 Jan 2009 09:34:37 GMT

      See http://stackoverflow.com/questions/493893/many-many-to-many-filters for a many many-to-many filter solution.

Comments are closed