June 2009 - Posts

LINQ: Quickly Create Dictionaries with ToDictionary

Donn Felker recently blogged about a neat little extension method in LINQ called Any().  If you simply want to know if a sequence contains any elements, many people use ".Count() > 0" which will walk the entire sequence to compute the count whereas .Any() will stop walking as soon as it finds a single element.  Easy and much more efficient.

It reminded me about another LINQ method I've used from time to time: ToDictionary().  This method will allow you to quickly create a dictionary from any IEnumerable<T>.  Let's start with some sample data that is in a List<T>:

IList<Person> people = new List<Person>
                       {
                        new Person {FirstName = "Bob", LastName = "Smith", SSN = "1"},
                        new Person {FirstName = "Jane", LastName = "Doe", SSN = "2"},
                        new Person {FirstName = "Mike", LastName = "Johnson", SSN = "3"}
                       };

Converting this to a dictionary is pretty trivial without LINQ.  Suppose we want to index these by SSN:

var d = new Dictionary<string, Person>();
foreach(var p in people)
{
    d.Add(p.SSN, p);
}

But why waste our time doing all that when we can simply use LINQ's ToDictionary()?  Just give it a lambda that selects the key and you're all set:

var indexedBySSN = people.ToDictionary(k => k.SSN);

Or perhaps you don't want the entire Person object.  Maybe you just want a collection of last names indexed by Social Security Number (SSN).  No problem --there's an overload that accepts two lambdas: one to select the key and one to select the value.

var lastNamesIndexedBySSN = people.ToDictionary(k => k.SSN, e => e.LastName);

There's also two more overloads that work the same as the two above, but allow you to also provide an IEqualityComparer<T> used to compare your keys.

Everyday I find more and more stuff in LINQ that helps me eliminate the mundane code and make my source more readable.

Technorati Tags: ,
Posted by PSteele | 1 comment(s)
Filed under: ,

SRT Presents Software Stimulus Lab to Support Economic, Business Growth

I've been so busy recently I totally forgot to blog about this.  We (SRT) are going to be hosting a Software Stimulus Lab at Automation Alley next Monday, June 15th.  We've got a press release that covers the details better than I can but the important things are:

  1. Seats are limited!  Register now – it's only $75.
  2. This is a hands-on workshop.  Bring your problems and we'll help you solve them in addition to teaching you how to solve them in the future.
  3. This is not about .NET.  Sure, we've got a great collection of C#, WPF and Silverlight folks, but we've also got some really talented Java, Scala and Python gurus.  And the tools aren't everything – learn techniques and methodologies to help drive your solutions to completion.

So check out the press release for all of the details and register here.

Technorati Tags: ,
Posted by PSteele | with no comments
Filed under:

ActiveRecord: Loading Records by Primary Key

I was reviewing some code today for a project that uses ActiveRecord.  One of the things that stood out for me was the use of a query to find objects by their primary key.  There's two reasons this raised a red flag for me:

  1. ActiveRecord already contains a built-in method for locating objects by their primary key: Find().
  2. Ayende just made a post about a month ago about why you shouldn't use queries to find objects by their primary key in NHibernate (which ActiveRecord uses under the hood).  You loose some optimizations that are built-in to NHibernate.  Read Ayende's post for more details.

So if you're an ActiveRecord user, how can you control which Session method to use – Get() or Load()?  Easy:

  • ActiveRecordBase.Find() uses Session.Load to load objects by primary key.
  • ActiveRecordBase.TryFind() uses Session.Get to load objects by primary key.

And if you're ever wondering how ActiveRecord works under the hood (or how some NHibernate concepts are used), grab the source and look around!

Technorati Tags: ,,
Posted by PSteele | with no comments
Filed under: ,

NHibernate Queries: HQL vs. Criteria API

As someone who did a lot of SQL a few years ago, I was immediately more comfortable using HQL for my ActiveRecord queries than the Criteria API.  But from a documentation/samples standpoint, I see a much higher percentage of the Criteria API being used vs. HQL.  I still use HQL most of the time.

This morning, Ayende had a post that answered the question I've always wondered: Which should I use?

Technorati Tags: ,,,
Posted by PSteele | with no comments
Filed under: ,
More Posts