다음을 통해 공유


ID 캐시에서 개체 검색

이 항목에서는 DataContext가 관리하는 ID 캐시에서 개체를 반환하는 LINQ to SQL 쿼리 유형에 대해 설명합니다.

LINQ to SQL에서 DataContext 개체를 관리하는 방법 중 하나는 쿼리가 실행될 때 ID 캐시에 개체 ID를 로깅하는 것입니다. 경우에 따라 LINQ to SQL은 데이터베이스에서 쿼리를 실행하기 전에 ID 캐시에서 개체를 검색하려고 시도합니다.

일반적으로 LINQ to SQL 쿼리가 ID 캐시에서 개체를 반환하려면 쿼리는 개체의 기본 키를 기반으로 해야 하며 단일 개체를 반환해야 합니다. 특히 쿼리는 아래에 표시된 일반적인 형식 중 하나여야 합니다.

비고

미리 컴파일된 쿼리는 ID 캐시에서 개체를 반환하지 않습니다. 미리 컴파일된 쿼리에 대한 자세한 내용은 쿼리 CompiledQuery 참조 하세요.

ID 캐시에서 개체를 검색하려면 쿼리가 다음 일반 양식 중 하나여야 합니다.

이러한 일반적인 형식에서는 Function1Function2predicate 다음과 같이 정의됩니다.

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)

참고하십시오