Quickly create a Dictionary using Linq to Objects

When working with collections or lists, sometimes you just need to get a simple dictionary.

There are a number of ways to do this, but the ToDictionary method seems promising.

For isntance:

  IList<Person> people = new List<Person>();
  people.Add(new Person 
      {
          FirstName = "Jim", 
          LastName="Schubert", 
          Age=29, 
          Phone="123-333-4444"
      });
  people.Add(new Person 
       {
           FirstName = "Joe", 
           LastName="Schubert", 
           Age=25, 
           Phone="800-999-1111"
       });
       
   Dictionary<int,string> names = people.ToDictionary(k => k.Age, v => v.FirstName);
   

There may not be many uses for this type of syntax, but simple transformations like this make Linq fun.