Using anonymous delegates with extensions to create your own LINQ extensions

Admittedly when I first started using LINQ , extensions, and lambda expressions I found them confusing because the concepts in them depart so far from the typical OO language structure.  Now I can’t live without them.  You really have a tremendous amount of flexibility of what kind of expressions you can create with LINQ as long as you don’t need the syntactic sugar.

For example, say I wanted a kind of filter that took an existing list and returned only the  values that existed above a particular threshold.  I could use a where clause that looked something like the following:

var list = new[] {0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 10000};

const double thresholdVal = 17;

// where clause

var newlist = list.Where(item => item > thresholdVal);

 

But say I wanted to generalize the threshold concept even more?  Let’s say I wanted a LINQ extension called Threshold that just took a predicate that determined the threshold and a threshold value.  That way I could have thresholds that meet minimum requirements, maximum requirements or any requirement I wanted.

 

The first thing I need to do is create an extension that could take my list and operate on it with the predicate.  The predicate would take a list item as its first parameter and the threshold as its second parameter.

 

using System;

using System.Collections.Generic;

using System.Linq;

 

namespace FunWithLINQ

{

      public static class Extensions

      {

        public static IEnumerable<T> Threshold<T>(this IEnumerable<T>
          queryList, Func<T, double, bool> thresholdFunction, double thresholdValue)

            {

                  return queryList.Where(o => thresholdFunction (o, thresholdValue));

            }

      }

}

 

Note that the extension function does not contain the actual threshold function, but instead gets the threshold function passed in as a delegate from the user.  All the extension method does is call the predicate that’s passed into the Threshold function on each element in the list.  We can take advantage of the LINQ Where clause to pick the items out of the list that return true for the predicate.

 

Implementing the Threshold function

 

To implement the Threshold function, we just call it on the list.  We pass the threshold function which takes an item in the list and a threshold value,  we also pass in the threshold as a third parameter .  The threshold will be applied against the predicate passed into the second parameter.

 

            var list = new[] {0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 10000};

                  const double thresholdVal = 17;

 

                  // custom threshold function

                 
var newlist = list.Threshold((listItem, threshold) => listItem > threshold, thresholdVal);

 

 

Note that the Threshold extension is completely equivalent to our Where clause in the results it produces.  The big difference is that the Threshold function is a filter very specific to a threshold value applied against a threshold function.

Ebook Download
View all
Learn
View all