Select method (anonymous type)
Description
The Select method projects each element of a sequence into a new form. Anonymous classes are classes without name, that contain a series of public read-only properties. They are generated on the fly by using the class{ property := value, … } syntax.
Sample
This sample transforms a list of Car objects to anonymous 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 => class{ 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 => class{ 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
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 class{ 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 class{ 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