Hämtar objekt från identitetscacheminnet
Det här avsnittet beskriver de typer av LINQ till SQL-frågor som returnerar ett objekt från identitetscacheminnet som hanteras av DataContext.
I LINQ till SQL är ett av sätten DataContext att hantera objekt genom att logga objektidentiteter i en identitetscache när frågor körs. I vissa fall försöker LINQ till SQL hämta ett objekt från identitetscacheminnet innan en fråga körs i databasen.
För att en LINQ till SQL-fråga ska returnera ett objekt från identitetscacheminnet måste frågan i allmänhet baseras på primärnyckeln för ett objekt och returnera ett enda objekt. I synnerhet måste frågan finnas i något av de allmänna formulär som visas nedan.
Kommentar
Förkompilerade frågor returnerar inte objekt från identitetscacheminnet. Mer information om förkompilerade frågor finns i CompiledQuery och Så här: Lagra och återanvända frågor.
En fråga måste finnas i något av följande allmänna formulär för att hämta ett objekt från identitetscacheminnet:
Table<TEntity>
.Function1(
predicate
)
Table<TEntity>
.Function1(
predicate
).Function2()
I dessa allmänna formulär Function1
definieras , Function2
och predicate
enligt följande.
Function1
kan vara något av följande:
Function2
kan vara något av följande:
predicate
måste vara ett uttryck där objektets primärnyckelegenskap är inställd på ett konstant värde. Om ett objekt har en primärnyckel som definierats av mer än en egenskap måste varje primärnyckelegenskap anges till ett konstant värde. Följande är exempel på formuläret predicate
måste vara:
c => c.PK == constant_value
c => c.PK1 == constant_value1 && c=> c.PK2 == constant_value2
Exempel
Följande kod innehåller exempel på de typer av LINQ till SQL-frågor som hämtar ett objekt från identitetscacheminnet.
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 {0}. ", 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)