ToDictionary method (with property)

Description

The ToDictionary method converts a sequence to a dictionary by selecting a property as key and a property as value.

Sample

This sample converts an list of persons to a dictionary using the name as key and the age as value.

var data := List<Person>{}{ ;
   Person{}{Name := "Jon Doe", Age := 40}, ;
   Person{}{Name := "Jane Doe", Age := 20}, ;
   Person{}{Name := "Joe Schmoe", Age := 30} ;
}

var result := data:ToDictionary({ q => q:Name }, { q => q:Age })

foreach var item in result:keys 
   Console.WriteLine(String.Format("Key: {0} - Value: {1}", item, result[item]))
next

Output

Key: Jon Doe - Value: 40
Key: Jane Doe - Value: 20
Key: Joe Schmoe - Value: 30

Complete sample

using System
using System.Linq
using System.Collections.Generic

procedure Execute() as void strict

   var data := List<Person>{}{ ;
      Person{}{Name := "Jon Doe", Age := 40}, ;
      Person{}{Name := "Jane Doe", Age := 20}, ;
      Person{}{Name := "Joe Schmoe", Age := 30} ;
   }

   var result := data:ToDictionary({ q => q:Name }, { q => q:Age })

   foreach var item in result:keys 
      Console.WriteLine(String.Format("Key: {0} - Value: {1}", item, result[item]))
   next
   return

class Person
   public property Name as string auto
   public property Age as int auto
end class