Log プロパティを使用して、クエリおよび変更処理用に生成された SQL コードを表示できます。 この方法は、LINQ to SQL の機能を理解したり、特定の問題をデバッグしたりするのに役立ちます。
例
次の例では、 Log プロパティを使用して、コードを実行する前にコンソール ウィンドウに SQL コードを表示します。 このプロパティは、クエリ、挿入、更新、および削除の各コマンドで使用できます。
コンソール ウィンドウの行は、次の Visual Basic または C# コードを実行したときに表示されます。
SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], [t0].[ContactT
itle], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Coun
try], [t0].[Phone], [t0].[Fax]
FROM [dbo].[Customers] AS [t0]
WHERE [t0].[City] = @p0
-- @p0: Input String (Size = 6; Prec = 0; Scale = 0) [London]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.20810.0
AROUT
BSBEV
CONSH
EASTC
NORTS
SEVES
db.Log = Console.Out;
IQueryable<Customer> custQuery =
from cust in db.Customers
where cust.City == "London"
select cust;
foreach(Customer custObj in custQuery)
{
Console.WriteLine(custObj.CustomerID);
}
db.Log = Console.Out
Dim custQuery = _
From cust In db.Customers _
Where cust.City = "London" _
Select cust
For Each custObj In custQuery
Console.WriteLine(custObj.CustomerID)
Next