LINQ, IQueryable and in between

I’m doing some thinking about our GUI, Data Access Layer, Entities and the way we should combine them. Looking a little further, LINQ is just a matter of time and effort until we’ll use this great infrastructure. I’ve downloaded a few movies about LINQ from Channel 9, and started to glance at my future. If you’re an architect, you should really take a look at Chatting about LINQ and ADO.NET Entities. This will give you a better insight about LINQ’s Architecture and where you should intervene if needed.


Our department uses a product we developed in-house named Code-Agent which allows us to generate our application based on our ERD. At current stage, we have our own infrastructure for manipulating data between our tiers, but it lacks some abilities which I really want to see in the near future.


This movie made me *think*.


But before I’ll carry on with my insights, let me talk about LINQ, IQueryable<T> and lamda expressions; Let’s start with a little example:


var results = from order in orders
              where order.Amount > 100
              select order.Name;


results will now contain all the orders with amount > 100.
The CLR transforms this query into method calls:


var results = orders.
                  Where(delegate(Order order) { return order.Amount>100; }).
                  Select(delegate(Order order) { return order.Name; });


Or via Lamda expression (kind of version 2 to anonymous method):


var results = orders.
               Where(order => order.Amount>100).
               Select(order => order.Name);


Seeing this kind of solution immediately made me think about writing our own LINQ wrapper for the meanwhile. Still, I can’t work on production code with LINQ as it really premature at current build but I can definitely build some sort of engine which implement the core principles of LINQ and in time remove my implementation, bit after bit. The next step was to look for IQueryable<T>, this baby inherit from IEnumerable<T> and let you do this magic I’ve demonstrated above (Where, Select, Group By, Order… you name it). I found a great post on this matter at Wayward Blog.


What’s the next step you might ask ?
I guess the answer is to emulate IQueryable<T> in our current infrastructure and aim for compatibility and consistency with LINQ.


Here is a quick implementation which pops out of my head:


EntityCollection<Order> orders = 
                  EntityCollection<Order>.
                     Where(delegate(Order o) { return o.Amount > 100; }).
                     Select(delegate(Order o) { return new { o.Name, o.Id }; });

foreach (Order o in orders)
{
    Console.WriteLine(“Order id: ” + o.Id + ” name: ” + o.Name);
}


EntityCollection<T> will inherit from IQueryable<T> and implement the basics.


It’s time to get into code-till-I’ll-bleed mode…