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 in the EDM.

  • CLR types supported by the EDM.

  • Inline collections.

  • Anonymous types.

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

Using AWEntities As New AdventureWorksEntities()
    Dim sales As ObjectQuery(Of SalesOrderHeader) = AWEntities.SalesOrderHeader

    Dim salesInfo = _
        From s In sales _
        Where s.TotalDue >= 200 _
        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
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{

    ObjectQuery<SalesOrderHeader> sales = AWEntities.SalesOrderHeader;

    var salesInfo =
        from s in sales
        where s.TotalDue >= 200
        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("");
    }
}

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

Using AWEntities As New AdventureWorksEntities()

    Dim salesInfo = _
        AWEntities.SalesOrderHeader _
        .Where(Function(s) s.TotalDue >= 200) _
        .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
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{

    var salesInfo =
        AWEntities.SalesOrderHeader
        .Where(s => s.TotalDue >= 200)
        .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("");
    }
}

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 SalesOrderNumber As String
    Public ShipDate As DateTime?
End Class
class MyOrder { public string SalesOrderNumber; public DateTime? ShipDate; }
Using AWEntities As New AdventureWorksEntities()
    Dim sales As ObjectQuery(Of SalesOrderHeader) = AWEntities.SalesOrderHeader

    Dim salesInfo = _
        From s In sales _
        Where s.TotalDue >= 200 _
        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
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
    ObjectQuery<SalesOrderHeader> sales = AWEntities.SalesOrderHeader;

    IQueryable<MyOrder> salesInfo =
        from s in sales
        where s.TotalDue >= 200
        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("");
    }
}

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

Using AWEntities As New AdventureWorksEntities()

    Dim salesInfo As IQueryable(Of MyOrder) = _
        AWEntities.SalesOrderHeader _
        .Where(Function(s) s.TotalDue >= 200) _
        .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
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
    IQueryable<MyOrder> salesInfo =
        AWEntities.SalesOrderHeader
        .Where(s => s.TotalDue >= 200)
        .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("");
    }
}

See Also

Concepts

Expressions in LINQ to Entities Queries