Select method (new type)
Description
The Select method projects each element of a sequence into a new form. The new objects are generated using the classname{}{ property := value, … } syntax.
Sample
This sample transforms a list of Car objects to CarWithOwner objects.
var data := List<Car>{}{ ;
Car{}{Manufacturer := "Alpha Romeo", Model := "Giulia", Owner := Person{}{Name := "Jon Doe", Age := 40}}, ;
Car{}{Manufacturer := "Fiat", Model := "Panda", Owner := Person{}{Name := "Jane Doe", Age := 20}} ;
}
var result := data:Select({ q => CarWithOwner{}{ Manufacturer := q:Manufacturer, Model := q:Model, OwnerName := q:Owner:Name } })
foreach var item in result
Console.WriteLine(String.Format("{0} {1} owned by {2}", item:Manufacturer, item:Model, item:OwnerName))
next
Output
Alpha Romeo Giulia owned by Jon Doe
Fiat Panda owned by Jane Doe
Complete sample
using System
using System.Linq
using System.Collections.Generic
procedure Execute() as void strict
var data := List<Car>{}{ ;
Car{}{Manufacturer := "Alpha Romeo", Model := "Giulia", Owner := Person{}{Name := "Jon Doe", Age := 40}}, ;
Car{}{Manufacturer := "Fiat", Model := "Panda", Owner := Person{}{Name := "Jane Doe", Age := 20}} ;
}
var result := data:Select({ q => CarWithOwner{}{ Manufacturer := q:Manufacturer, Model := q:Model, OwnerName := q:Owner:Name } })
foreach var item in result
Console.WriteLine(String.Format("{0} {1} owned by {2}", item:Manufacturer, item:Model, item:OwnerName))
next
return
class Person
public property Name as string auto
public property Age as int auto
end class
class Car
public property Manufacturer as string auto
public property Model as string auto
public property Owner as Person auto
end class
class CarWithOwner
public property Manufacturer as string auto
public property Model as string auto
public property OwnerName as string auto
end class
var data := List<Car>{}{ ;
Car{}{Manufacturer := "Alpha Romeo", Model := "Giulia", Owner := Person{}{Name := "Jon Doe", Age := 40}}, ;
Car{}{Manufacturer := "Fiat", Model := "Panda", Owner := Person{}{Name := "Jane Doe", Age := 20}} ;
}
var result := from q in data ;
select CarWithOwner{}{ Manufacturer:= q:Manufacturer, Model := q:Model, OwnerName := q:Owner:Name }
foreach var item in result
Console.WriteLine(String.Format("{0} {1} owned by {2}", item:Manufacturer, item:Model, item:OwnerName))
next
Output
Alpha Romeo Giulia owned by Jon Doe
Fiat Panda owned by Jane Doe
Complete sample
using System
using System.Linq
using System.Collections.Generic
procedure Execute() as void strict
var data := List<Car>{}{ ;
Car{}{Manufacturer := "Alpha Romeo", Model := "Giulia", Owner := Person{}{Name := "Jon Doe", Age := 40}}, ;
Car{}{Manufacturer := "Fiat", Model := "Panda", Owner := Person{}{Name := "Jane Doe", Age := 20}} ;
}
var result := from q in data ;
select CarWithOwner{}{ Manufacturer:= q:Manufacturer, Model := q:Model, OwnerName := q:Owner:Name }
foreach var item in result
Console.WriteLine(String.Format("{0} {1} owned by {2}", item:Manufacturer, item:Model, item:OwnerName))
next
return
class Person
public property Name as string auto
public property Age as int auto
end class
class Car
public property Manufacturer as string auto
public property Model as string auto
public property Owner as Person auto
end class
class CarWithOwner
public property Manufacturer as string auto
public property Model as string auto
public property OwnerName as string auto
end class