Edit

Share via


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:

Decimal totalDue = 200;
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    var salesInfo =
        from s in context.SalesOrderHeaders
        where s.TotalDue >= totalDue
        select new { s.SalesOrderNumber, s.TotalDue };

    Console.WriteLine("Sales order numbers:");
    foreach (var sale in salesInfo)
    {
        Console.WriteLine("Order number: " + sale.SalesOrderNumber);
        Console.WriteLine("Total due: " + sale.TotalDue);
        Console.WriteLine("");
    }
}
Dim totalDue = 200
Using context As New AdventureWorksEntities()
    Dim salesInfo = _
        From s In context.SalesOrderHeaders _
        Where s.TotalDue >= totalDue _
        Select New With {s.SalesOrderNumber, s.TotalDue}

    Console.WriteLine("Sales order numbers:")
    For Each sale In salesInfo
        Console.WriteLine("Order number: " & sale.SalesOrderNumber)
        Console.WriteLine("Total due: " & sale.TotalDue)
        Console.WriteLine("")
    Next
End Using

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

Decimal totalDue = 200;
using (AdventureWorksEntities context = new AdventureWorksEntities())
{

    var salesInfo =
        context.SalesOrderHeaders
        .Where(s => s.TotalDue >= totalDue)
        .Select(s => new { s.SalesOrderNumber, s.TotalDue });

    Console.WriteLine("Sales order numbers:");
    foreach (var sale in salesInfo)
    {
        Console.WriteLine("Order number: " + sale.SalesOrderNumber);
        Console.WriteLine("Total due: " + sale.TotalDue);
        Console.WriteLine("");
    }
}
Dim totalDue = 200
Using context As New AdventureWorksEntities()

    Dim salesInfo = _
        context.SalesOrderHeaders _
        .Where(Function(s) s.TotalDue >= totalDue) _
        .Select(Function(s) New With {s.SalesOrderNumber, s.TotalDue})

    Console.WriteLine("Sales order numbers:")
    For Each sale In salesInfo
        Console.WriteLine("Order number: " & sale.SalesOrderNumber)
        Console.WriteLine("Total due: " & sale.TotalDue)
        Console.WriteLine("")
    Next
End Using

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:

class MyOrder { public string SalesOrderNumber; public DateTime? ShipDate; }
Class MyOrder
    Public SalesOrderNumber As String
    Public ShipDate As DateTime?
End Class
Decimal totalDue = 200;
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    IQueryable<MyOrder> salesInfo =
        from s in context.SalesOrderHeaders
        where s.TotalDue >= totalDue
        select new MyOrder
        {
            SalesOrderNumber = s.SalesOrderNumber,
            ShipDate = s.ShipDate
        };

    Console.WriteLine("Sales order info:");
    foreach (MyOrder order in salesInfo)
    {
        Console.WriteLine("Order number: " + order.SalesOrderNumber);
        Console.WriteLine("Ship date: " + order.ShipDate);
        Console.WriteLine("");
    }
}
Dim totalDue = 200
Using context As New AdventureWorksEntities()
    Dim salesInfo = _
        From s In context.SalesOrderHeaders _
        Where s.TotalDue >= totalDue _
        Select New MyOrder With _
               { _
                   .SalesOrderNumber = s.SalesOrderNumber, _
                   .ShipDate = s.ShipDate _
               }

    Console.WriteLine("Sales order info:")
    For Each order As MyOrder In salesInfo
        Console.WriteLine("Order number: " & order.SalesOrderNumber)
        Console.WriteLine("Ship date: " & order.ShipDate)
        Console.WriteLine("")
    Next
End Using

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

Decimal totalDue = 200;
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    IQueryable<MyOrder> salesInfo =
        context.SalesOrderHeaders
        .Where(s => s.TotalDue >= totalDue)
        .Select(s => new MyOrder
        {
            SalesOrderNumber = s.SalesOrderNumber,
            ShipDate = s.ShipDate
        });

    Console.WriteLine("Sales order info:");
    foreach (MyOrder order in salesInfo)
    {
        Console.WriteLine("Order number: " + order.SalesOrderNumber);
        Console.WriteLine("Ship date: " + order.ShipDate);
        Console.WriteLine("");
    }
}
Dim totalDue = 200
Using context As New AdventureWorksEntities()

    Dim salesInfo As IQueryable(Of MyOrder) = _
        context.SalesOrderHeaders _
        .Where(Function(s) s.TotalDue >= totalDue) _
        .Select(Function(s) New MyOrder With _
               { _
                   .SalesOrderNumber = s.SalesOrderNumber, _
                   .ShipDate = s.ShipDate _
               })

    Console.WriteLine("Sales order info:")
    For Each order As MyOrder In salesInfo
        Console.WriteLine("Order number: " & order.SalesOrderNumber)
        Console.WriteLine("Ship date: " & order.ShipDate)
        Console.WriteLine("")
    Next
End Using

See also