ToDictionary method (with objects)
Description
The ToDictionary method converts a sequence to a dictionary by selecting a property as key.
Sample
This sample converts an list of persons to a dictionary using the name as key and the object 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 })
foreach var item in result:keys
Console.WriteLine(String.Format("Key: {0} - Value: {1}", item, result[item]:Age))
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 })
foreach var item in result:keys
Console.WriteLine(String.Format("Key: {0} - Value: {1}", item, result[item]:Age))
next
return
class Person
public property Name as string auto
public property Age as int auto
end class