Back for a Bit

So I was at work today and I am currently on the bench, meaning not actually assigned to a project, though I am supporting both Spout as well as the Columbus Dispatch project. And I noticed my actual website, seems to have lost its domain. I think this is a good point to recreate it anew. I am not really concerend with losing the domain, but I want to be able to create a new site, so for a bit I will be using to blog until my Nusoft one is ready.

So lets talk about LINQ, which has become a favorite subject of mine, mainly for the verbosity it allows code to have. While its obvious it is still incomplete, Microsoft has made great progress on making life for the programmer easier. So anyone who has heard of mine as probably seen the examples that have been posted such as:

var query = from s in db.Series
select s;

For the uninitiated, this an example using LINQ to SQL, although it could really refer to any supported LINQ context. In this case db is a generated reference to our database via a set of classes generated to represent the database in a clear and strongly typed fashion. I wont spend a lot of time explaining this, Ill save it for another post. Basically think of it this way, db.Series is a collection of objects, and I am select each object and naming it ‘s’. I then use the ‘var’ keyword to infer the type of the return based on the evaluation of the right hand side.

Simply put, var has no type until we finish executing the query, then the CLR determines the type through inference.

So enough introduction, how this useful, what can it do?

In theory, we can use LINQ to generate the vast majority of the tedious grunt work performed in the business layer of an application. LINQ will generate the classes to represent our tables, properties to references the fields and will “connect the dots” with our one to many relationships. No many to many at this point, but Ill show you how to get around that.

Now you might be thinking, this is too good to be true, surely Ill have to do some work. And you would be correct. Whether its intended or LINQ is incomplete, set against something like the Rails framework, it appears incomplete. For example, in so far as I have found, counting low level hierarchical data from the top level cannot be done without creating a query. In Rails, you can add a couple statements to the needed models and this information is provided. In LINQ, this was the solution I came up with:

var query = from result in (
from s in db.Series
select new
SeriesId = s.seriesId,
Name =,
EpCount =
from ep in db.Episodes
join se in db.Seasons
on ep.seasonId equals se.seasonId into joinedSeasons
from js in joinedSeasons
where js.seriesId == s.seriesId
select js
orderby result.EpCount descending
select result;

So let me give some background to this data. This is from my AnimeManager program that I am redesigning using LINQ, the previous version used RoR. All main data is organized as such: Series, which have Seasons, and Seasons which have Episodes. This code will return to me an anonymously typed object containing a specific series, its ids, and how many episodes are stored in its seasons.

While it may look complex, when broken down its actually very simple to understand. A couple things to note is the way I am using “joined tables”. When your writing LINQ queries, it is very much like writing normal program code in the way you think. You can use parenthesis to denote what should be evaluated in what order, and then know that inside the parentheseis will normally return a Queryable object, which supports various methods, you can think of these as table objects, though they are more like anonymous tables. You can see the use of the table method Count which I use to generate the count of the episodes in the subquery.

The way we write and think about LINQ is mostly backwards from the way we write most queries, this is mainly due to LINQs roots in XQuery. The key thing to remember about LINQ queries is the ordering and understand exactly what you have. So far, the intellisense has been very helpful within Orcas Beta 2 and is a good way to gain an understanding as to what variables you have access to at any given time.

I plan to post more about LINQ throughout the week, toodles


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s