Export (0) Print
Expand All

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("");
}
}
    

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("");
}
}
    

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; }
    

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("");
}
}
    

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("");
}
}
    

See Also




Build Date:

2012-10-01
Show:
© 2014 Microsoft