Initialization Expressions

 

An initialization expression initializes a new object. Most initialization expressions are supported, including most new C# 3.0 and Visual Basic 9.0 initialization expressions. The following types can be initialized and returned by a LINQ to Entities query:

  • A collection of zero or more typed entity objects or a projection of complex types that are defined in the conceptual model.

  • CLR types supported by the Entity Framework.

  • Inline collections.

  • Anonymous types.

Anonymous type initialization is shown in the following example in query expression syntax:

[!CODE [DP L2E Conceptual Examples#AnonymousTypeInitialization](../CodeSnippet/VS_Snippets_Data/DP L2E Conceptual Examples#anonymoustypeinitialization)]

The following example in method-based query syntax shows anonymous type initialization:

[!CODE [DP L2E Conceptual Examples#AnonymousTypeInitialization_MQ](../CodeSnippet/VS_Snippets_Data/DP L2E Conceptual Examples#anonymoustypeinitialization_mq)]

User-defined class initialization is also supported. The C# 3.0 and Visual Basic 9.0 initialization pattern is supported and assumes that the property getter and setter are symmetric. The following example in query expression syntax shows a custom class being initialized in the query:

[!CODE [DP L2E Conceptual Examples#MyOrder](../CodeSnippet/VS_Snippets_Data/DP L2E Conceptual Examples#myorder)]

[!CODE [DP L2E Conceptual Examples#TypeInitialization](../CodeSnippet/VS_Snippets_Data/DP L2E Conceptual Examples#typeinitialization)]

The following example in method-based query syntax shows a custom class being initialized in the query:

[!CODE [DP L2E Conceptual Examples#TypeInitialization_MQ](../CodeSnippet/VS_Snippets_Data/DP L2E Conceptual Examples#typeinitialization_mq)]

Expressions in LINQ to Entities Queries

Show: