Christian Nagel's OneNotes

.NET Training, Consulting, Coaching - C#, Web Services, Enterprise Services, ASP.NET, Whidbey, Longhorn and More!

Sponsors

Affiliations

Books I've written

INETA UG Leaders

My Blogroll

Predicates with .NET 2.0

In a previous blog entry I've shown the transformation of the Accumulate method from a traditional implementation to a generic implementation where the action is passed. This was the result of the last version that does something to every object in a collection:

public static TSum Accumulate<TObj, TSum>(
  
IEnumerable<TObj> coll, Action<TObj, TSum> action)
{
   TSum sum = default(TSum);

   foreach (TObj obj in coll)
   {
      sum = action(obj, sum);
   }
   return sum;
}

And this is how this method can be invoked with an anonymous delegate:

decimal amount = Algorithm.Accumulate<Account, decimal>(
   accounts,
   delegate(Account a, decimal sum)
   {
      return a.Balance + sum;
   });

Now I'm extending the implementation of the Accumulate method by using a predicate. A predicate is a method that returns a boolean value. .NET 2.0 defines this generic predicate delegate:

public delegate bool Predicate<T>(T obj);

Such a predicate can be used with the AccumulateIf method to only work with these objects of a collection where the predicate returns true:

public static TSum AccumulateIf<TObj, TSum>(
  
IEnumerable<TObj> coll,
  
Action<TObj, TSum> action
   Predicate<TObj> filter)
{
   TSum sum = default(TSum);

   foreach (TObj obj in coll)
   {
      if (filter(obj))

         sum = action(obj, sum);
   }
   return sum;
}

Now the predicate implementation - the algorithm when the accumulation should happen - can be passed with the method AccumulateIf as anonymous method. In the example the action only happens if the balance has a value greater than 2500.

decimal amount = Algorithm.AccumulateIf<Account, decimal>(
   accounts,
   delegate(Account a, decimal sum) // Action
   {
      return a.Balance + sum;
   },
   delegate (Account a)  // Predicate
   {
      return a.Balance > 2500;

   }
  
);

Christian

Posted: Mar 10 2005, 10:06 AM by CNagel | with 9 comment(s)
Filed under: ,

Comments

TrackBack said:

# April 19, 2005 9:58 PM

TrackBack said:

# April 27, 2005 10:35 AM

TrackBack said:

I am currently preparing my developer track talks for the local Big&gt;Days 2005 road show and - oh what...
# May 9, 2005 4:24 AM

Greg said:

A predicate only really makes sense if you are to use it multiple times.  A one-use predicate is less maintainable than just standard, dumb level, looping since all of the code for the loop is in the same place.

# March 14, 2008 2:40 PM

weblogs.asp.net said:

391616.. Retweeted it :)

# May 15, 2011 6:19 PM

Parks said:

in our extended family, we've been working on this in earnest over the last few years. One variation is the game called White Elephant. Favorite thing about the Holidays.

# January 28, 2013 10:52 PM

Heffner said:

3. Holidays are when many of the presents are purchased and shared.

The oranges aren't really suited to eating - it's more like you

are drinking fruit than eating it, way too juicy.

# January 28, 2013 11:36 PM

cheap nba jerseys said:

very best zippered pants pocket too as a

# April 20, 2013 4:00 AM

cheap jordans said:

owning a Six.A few millimeter lower. The item

# April 20, 2013 4:01 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)