Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Wednesday, October 11, 2006

.NET Aspiring.Query - Extra LINQ Extension Methods

.NET Aspiring.Query - Extra LINQ Extension Methods

After experimenting with System.Query operators I found that the behavior of throwing an exception when a sequence was empty limiting (eg. the user un-checks all line items in a grid, the LINQ where clause returns an empty set and I needed to put Try/Except's around all of my queries, I decided I needed a variant of Min and Max that allowed a default value to be returned under these conditions instead. I also found that I was constantly blowing up the Sum operator because it returned an int type for a collection of int values; it doesn't take much to overflow that limit so I wrote a LongSum operator.

I'll keep adding to this library as I discover behavior or new extension methods that suit my needs. Feel free to download, comment and suggest any others you might like to see.

Download the first version here: Aspiring.Query version 1.0

All the methods are unit-tested, and because I had unit-tests for the standard query operators - I'm extremely confident that they share behavior with the Microsoft System.Query family. BUT: You are getting these for free, and it is up to you to check that are in fact what you need!

Here is a snippet of the Readme.txt file included in the solution and zip file. Just un-zip the file you download and open with Visual Studio 2005 (see pre-requisites below):

Aspiring.Query V1.0

This is a progressive project to add additional extension methods that might be
useful for LINQ over Objects.

The standard query operators offer a great start to querying and manipulating
object collections, but they are just the start.

If you have any ideas, feel free to email me; Better still, write it yourself
and email that to me for addition.

If you do contribute, the only criteria is that all methods are properly
documented and fully unit tested. As for style, I'm just copying that supplied
in Sequence.cs from Microsoft which has the System.Query namespace and the
existing standard query operators live.

Post a Comment