初始化表达式
初始化表达式用于初始化新的对象。 大多数初始化表达式都受到支持,其中包括大多数新的 C# 3.0 和 Visual Basic 9.0 初始化表达式。 LINQ to Entities 查询可以初始化并返回以下类型:
在概念模型中定义的零个或多个类型化实体对象的集合或复杂类型的投影。
实体框架支持的 CLR 类型。
内联集合。
匿名类型。
下面的示例中以查询表达式语法演示了匿名类型初始化:
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
下面的基于方法的查询语法示例说明了匿名类型初始化:
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
用户定义类的初始化也受到支持。 C# 3.0 和 Visual Basic 9.0 初始化模式受到支持,并且假定属性获取程序和设置程序是对称的。 下面的查询表达式语法示例说明了如何在查询中初始化自定义类:
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
下面的基于方法的查询语法示例说明了如何在查询中初始化自定义类:
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