初期化式
初期化式は、新しいオブジェクトを初期化します。初期化式は、ほとんどの新しい C# 3.0 および Visual Basic 9.0 初期化式を含め、ほとんどがサポートされています。次の型は、LINQ to Entities クエリによって初期化して返すことができます。
0 個以上のエンティティ オブジェクトのコレクション、または EDM の複合型の投影。
EDM でサポートされる CLR 型。
インライン コレクション。
匿名型。
クエリ式構文の次の例では、匿名型の初期化を示します。
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("");
}
}
メソッドベースのクエリ構文の次の例では、匿名型の初期化を示します。
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("");
}
}
ユーザー定義クラスの初期化もサポートされています。C# 3.0 および Visual Basic 9.0 の初期化パターンがサポートされており、getter と setter のプロパティは対称であることが前提となります。クエリ式構文の次の例では、クエリ内で初期化されるカスタム クラスを示します。
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("");
}
}
メソッドベース クエリ構文の次の例では、クエリ内で初期化されるカスタム クラスを示します。
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("");
}
}