Visual Basic supports anonymous types, which enable you to create
objects without writing a class definition for the data type. Instead,
the compiler generates a class for you. The class has no usable name,
inherits directly from Object,
and contains the properties you specify in declaring the object.
Because the name of the data type is not specified, it is referred to as
an anonymous type.
The following example declares and creates variable product as an instance of an anonymous type that has two properties, Name and Price.
In the following examples, products is a list of product objects, each of which has many properties. Variable namePriceQuery holds the definition of a query that, when it is executed, returns a collection of instances of an anonymous type that has two properties, Name and Price.
The following example declares and creates variable product as an instance of an anonymous type that has two properties, Name and Price.
' Variable product is an instance of a simple anonymous type. Dim product = New With {Key .Name = "paperclips", .Price = 1.29}
A query expression uses anonymous types to combine columns of data selected by a query. You cannot define the type of the result in advance, because you cannot predict the columns a particular query might select. Anonymous types enable you to write a query that selects any number of columns, in any order. The compiler creates a data type that matches the specified properties and the specified order.
In the following examples, products is a list of product objects, each of which has many properties. Variable namePriceQuery holds the definition of a query that, when it is executed, returns a collection of instances of an anonymous type that has two properties, Name and Price.
Dim namePriceQuery = From prod In products Select prod.Name, prod.Price
No comments:
Post a Comment