LINQ is amazing!

I'm just starting to get my head around what LINQ is all about, and I have to say I love it! What's really struck me is the power of LINQ to Objects to concisely and accurately describe the intention of the code without the usual complexity of loops and ifs. To give you a example, I'm putting together a simple proof of concept publish/subscribe message bus in .NET at the moment. It's at the very early stages at the moment, and I'm sure it'll be the subject of future posts, but one of the first things I needed to do is match a published message to a set of subscriptions to identify the subscribers to activate.

The original version of the code involved a foreach loop and a number of if conditions to find the correct subscriptions for the published message. It worked just fine, and read fairly well too. But just I think the LINQ version speaks for itself:

var subscriptionsToActivate = from subcriptionObject in subscriptions
           where subcriptionObject is Subscription<T>
           let subscription = (Subscription<T>)subcriptionObject
           where subscription.ShouldActivateFor(message)
           select subscription;

Nuff said!

What others are saying

Comments are closed for this post