linq to dictionary code example

Example 1: c# linq to dictionary

var dict = TableObj.ToDictionary( t => t.Key, t => t.TimeStamp );

Example 2: function toDictionary

var propertyDictionary = object.ToDictionary(x => x.Property1, x => x.Property2);

Example 3: c# new dictionary linq

var result = 
  // as Jon Skeet pointed out, OrderBy is useless here, I just leave it 
  // show how to use OrderBy in a LINQ query
  myClassCollection.OrderBy(mc => mc.SomePropToSortOn)
                   .ToDictionary(mc => mc.KeyProp.ToString(), 
                                 mc => mc.ValueProp.ToString(), 
                                 StringComparer.OrdinalIgnoreCase);

Tags:

Misc Example