Omer van Kloeten's .NET Zen

Programming is life, the rest is mere details

News

Omer van Kloeten's Facebook profile

Omer has been professionally developing applications over the past 8 years, both at the IDF’s IT corps and later at the Sela Technology Center, but has had the programming bug ever since he can remember himself.
As a senior developer at NuConomy, a leading web analytics and advertising startup, he leads a wide range of technologies for its flagship products.

Get Firefox


powered by Dapper 

.NET Resources

Articles :: CodeDom

Articles :: nGineer

Culture

Projects

Linq: ContainsAtLeast and AggregationOrDefault

I've created a couple of useful extension methods that I like to use with Linq, so here they are:

ContainsAtLeast

I've noticed that there's no way to find out whether a collection has at least X items. The following takes the collection, tries to take X items from it and asks whether it succeeded or not:

public static bool ContainsAtLeast<T>(this IEnumerable<T> enumeration,
int count) { // Check to see that enumeration is not null if (enumeration == null) throw new ArgumentNullException("enumeration"); return (from t in enumeration.Take(count) select t) .Count() == count; }

AggregationOrDefault

When I need the first item in a collection, I love using First when I need an exception and FirstOrDefault when I don't. However, this doesn't exist for aggregations and if your collection is empty, Max, Min, etc. will throw an exception.

public static T AggregationOrDefault<T>(this IEnumerable<T> enumeration,
Func<IEnumerable<T>, T> aggregationMethod) { // Check to see that enumeration is not null if (enumeration == null) throw new ArgumentNullException("enumeration"); // Check to see that aggregationMethod is not null if (aggregationMethod == null) throw new ArgumentNullException("aggregationMethod"); if (!enumeration.ContainsAtLeast(1)) return default(T); return aggregationMethod(enumeration); }

What this does is take a collection and an aggregation method, checks whether there's at least one item and applies the method. This is very useful, because most aggregation methods that take only one parameter - the collection, are not generic, so that would mean, just like in Linq itself, creating a copy of this method for every numeric type (as an example, see Max<T> compared to Max).

How to use it? Simple:

myEnumeration.AggregationOrDefault(Enumerable.Max);

In this case, sending the Max method to AggregationOrDefault.

Comments

FransBouma said:

Why do you Take(count) from the enumeration ? You can just do Count on the enumeration and compare it with the passed in value, no?

# April 6, 2008 5:47 AM

Omer van Kloeten said:

I don't want to iterate over the entire collection. If it's 100000 items long... :/

# April 6, 2008 12:51 PM

John said:

I was just going to say, you'd enumerate the entire collection if you used Count before Take.

# April 6, 2008 1:00 PM

Dew Drop - April 7, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - April 7, 2008 | Alvin Ashcraft's Morning Dew

# April 7, 2008 8:02 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)