Wednesday, 31 May 2006

When I first saw Matt's blog entry, I thought this guy has gone crazy. They have just released the new CTP of LINQ, which should have been a source of great stress for him, whatever. He has gone crazy for sure. Shit happens.

Unfortunately I’m not the kind who writes yippees and hurrahs, but believe me, now I feel exactly the same as Matt - whoopee! And I also know the ticket to getting into this state: LINQ.

I have to keep my text short (huhuuu), so just look at this:

foreach (var customer in 		
         from c in dataDomain.Extent<Customer>() 
         where c.City == "London" || c.CompanyName.Length < 5
         orderby c.ContactName
         select c)
{
    Console.WriteLine("{0} - {1}", 
        customer.ContactName, 
        customer.City);
}

Yes, I have the first version of Genome LINQ (G-LINQ?)!

I downloaded the new stuff a few days ago. Thanks to the LINQ infrastructure, which proved to be extensible, and the OqlExpression-tree representation of Genome, which can be easily transformed from the System.Expressions model provided by LINQ, I was able to create the first working prototype within a day.

I have created the necessary infrastructure for filtering, ordering and projecting the Set<T> type (which is also new, and will become final in Genome 3.0). One of the cool things about LINQ is that it always tries to find an implementation for the task you want to accomplish (hah!). Therefore, even though I have not provided implementation for grouping, you can still write

foreach (var customerGroup in 		
         from c in dataDomain.Extent<Customer>() 
         where c.City == "London" || c.CompanyName.Length < 5
         group c by c.City into cityGroups
         select cityGroups)
{
    Console.WriteLine("City: {0}", customerGroup.Key);

    foreach (var customer in customerGroup)
    {
        Console.WriteLine("{0} - {1}", 
            customer.ContactName, 
            customer.City);
    }
}

The query part with the filtering will be evaluated by Genome and the database server and the rest will be done in the memory. So it works! Cool! I like it very much! Yippee!

Subqueries? No problem:

foreach (var customer in 		
         from c in dataDomain.Extent<Customer>() 
         where c.Orders.Where(o => o.ShipCity == "London").Count > 3
         select c)
{
    Console.WriteLine("{0} - {1}", 
        customer.ContactName, 
        customer.City);
}

I also ran into problems, however. It seems that extension methods for generic types do not work well in some cases. I am unable to get my order implementation to work with the new query syntax (orderby) if there is more than one order clause. Also, LINQ syntax (as expected) completely confused ReSharper, but these are just minor things.

LINQ rocks. Cooool.

I’ll put the tech preview I made for Genome LINQ integration online within the next few days. I'm already excited about your feedback. Until then, you can have a look at the new LINQ CTP (downloadable from the LINQ product page). Bravely enough, I have installed it directly on my notebook. There are install / uninstall scripts in the deployment directory that you can use to switch on/off the C# 3 compiler that seem to work fine so far.

Bee crazy! Kukurikuuu…

Posted by Gáspár