Saturday, July 28, 2007 5:50 PM bart

LINQ-SQO v0.9.2 (for Orcas Beta 2) RTW

With the advent of Orcas Beta 2 earlier this week, it's about time to ship an update to the LINQ-SQO project. You can download the 0.9.2 release right now from CodePlex at http://www.codeplex.com/LINQSQO. The goal of this project is to provide a custom implementation of the LINQ to Objects Standard Query Operators (SQO). Basically this means you should be able to compile and run any piece of LINQ to Objects code against the LINQ-SQO API just by changing the "using System.Linq" namespace import with "using BdsSoft.Linq", with the same runtime behavior of course.

The project implements all of the System.Linq.Enumerable (extension) methods with all of their overloads in a similar static class called BdsSoft.Linq.Enumerable. It ships with a total of 148 (simple) unit tests to check functionality. If you want to make sure all of the operators are there, you could eat LINQ's own dogfood to match both implementations:

var res1 = from mi in typeof(System.Linq.Enumerable).GetMethods()
           orderby mi.Name
           group mi by mi.Name into g
           select new { Name = g.Key, Overloads = g.Count() };
var res2 = from mi in typeof(BdsSoft.Linq.Enumerable).GetMethods()
           orderby mi.Name
           group mi by mi.Name into g
           select new { Name = g.Key, Overloads = g.Count() };

if (!res1.SequenceEqual(res2))
{
    Console.WriteLine("Implementation doesn't match the official LINQ to Objects standard query operators. Mismatches are:");

    var mismatches = from m1 in res1
                     join m2 in res2 on m1.Name equals m2.Name
                     where m1.Overloads != m2.Overloads
                     select new { m1.Name, Theirs = m1.Overloads, Ours = m2.Overloads };

    foreach (var m in mismatches)
        Console.WriteLine("{0} has {1} overloads instead of {2}", m.Name, m.Ours, m.Theirs);
}

I use this code myself to check for new operator overloads that appear in newer Orcas builds. From Orcas Beta 1 to Orcas Beta 2 five methods were added: four GroupBy overloads and one SelectMany overload:

public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, IEnumerable<TSource>, TResult> resultSelector)
public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, IEnumerable<TSource>, TResult> resultSelector, IEqualityComparer<TKey> comparer)
public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, IEnumerable<TSource>, TResult> resultSelector)
public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, IEnumerable<TSource>, TResult> resultSelector, IEqualityComparer<TKey> comparer)

public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(this IEnumerable<TSource> source, Func<TSource, int, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)

It might be a nice exercise to play the human compiler on the code fragment above to make sure you're (still) familiar with extension method stuff and the query operators (Q: which operators play a role in the code fragment above + which overloads do get called?).

WARNING: Notice this implementation isn't meant to be used in production, it's rather a reference implementation that could help you to understand how the query operators work internally.

Enjoy!

Del.icio.us | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

Filed under: ,

Comments

# University Update-C#-LINQ-SQO v0.9.2 (for Orcas Beta 2) RTW

Pingback from  University Update-C#-LINQ-SQO v0.9.2 (for Orcas Beta 2) RTW

# http://bartdesmet.net/blogs/bart/archive/2007/07/28/linq-sqo-v0-9-2-for-orcas-beta-2-rtw.aspx