次の方法で共有


ID キャッシュからのオブジェクトの取得

このトピックでは、 DataContextによって管理される ID キャッシュからオブジェクトを返す LINQ to SQL クエリの種類について説明します。

LINQ to SQL では、 DataContext がオブジェクトを管理する方法の 1 つは、クエリの実行時に ID キャッシュにオブジェクト ID をログに記録することです。 場合によっては、LINQ to SQL は、データベースでクエリを実行する前に、ID キャッシュからオブジェクトの取得を試みます。

一般に、LINQ to SQL クエリで ID キャッシュからオブジェクトを返すには、クエリはオブジェクトの主キーに基づいており、1 つのオブジェクトを返す必要があります。 特に、クエリは次に示す一般的な形式のいずれかである必要があります。

事前コンパイル済みクエリは、ID キャッシュからオブジェクトを返しません。 事前コンパイル済みクエリの詳細については、「 CompiledQuery方法: クエリを格納および再利用する」を参照してください。

ID キャッシュからオブジェクトを取得するには、クエリが次のいずれかの一般的な形式である必要があります。

これらの一般的な形式では、 Function1Function2、および predicate は次のように定義されます。

Function1 には、次のいずれかを指定できます。

Function2 には、次のいずれかを指定できます。

predicate は、オブジェクトの主キー プロパティが定数値に設定されている式である必要があります。 オブジェクトに複数のプロパティによって定義された主キーがある場合は、各主キー プロパティを定数値に設定する必要があります。 次に、predicate が取るべき形の例を示します。

  • c => c.PK == constant_value

  • c => c.PK1 == constant_value1 && c=> c.PK2 == constant_value2

次のコードは、ID キャッシュからオブジェクトを取得する LINQ to SQL クエリの種類の例を示しています。

NorthwindDataContext context = new NorthwindDataContext();

// This query does not retrieve an object from
// the query cache because it is the first query.
// There are no objects in the cache.
var a = context.Customers.First();
Console.WriteLine($"First query gets customer {a.CustomerID}. ");

// This query returns an object from the query cache.
var b = context.Customers.Where(c => c.CustomerID == a.CustomerID);
foreach (var customer in b )
{
    Console.WriteLine(customer.CustomerID);
}

// This query returns an object from the identity cache.
// Note that calling FirstOrDefault(), Single(), or SingleOrDefault()
// instead of First() will also return an object from the cache.
var x = context.Customers.
    Where(c => c.CustomerID == a.CustomerID).
    First();
Console.WriteLine(x.CustomerID);

// This query returns an object from the identity cache.
// Note that calling FirstOrDefault(), Single(), or SingleOrDefault()
// instead of First() (each with the same predicate) will also
// return an object from the cache.
var y = context.Customers.First(c => c.CustomerID == a.CustomerID);
Console.WriteLine(y.CustomerID);
Dim context As New NorthwindDataContext()

' This query does not retrieve an object from
' the query cache because it is the first query.
' There are no objects in the cache. 
Dim a = context.Customers.First()
Console.WriteLine("First query gets customer {0}. ", a.CustomerID)

' This query returns an object from the query cache.
Dim b = context.Customers.Where(Function(c) c.CustomerID = a.CustomerID)
For Each customer In b
    Console.WriteLine(customer.CustomerID)
Next

' This query returns an object from the identity cache.
' Note that calling FirstOrDefault(), Single(), or SingleOrDefault()
' instead of First() will also return an object from the cache.
Dim x = context.Customers. _
    Where(Function(c) c.CustomerID = a.CustomerID). _
    First()
Console.WriteLine(x.CustomerID)

' This query returns an object from the identity cache.
' Note that calling FirstOrDefault(), Single(), or SingleOrDefault()
' instead of First() (each with the same predicate) will also
' return an object from the cache.
Dim y = context.Customers.First(Function(c) c.CustomerID = a.CustomerID)
Console.WriteLine(y.CustomerID)

こちらも参照ください