從識別快取擷取物件
本主題描述 LINQ to SQL 查詢的類型,這些查詢可從 DataContext 所管理的識別快取傳回物件。
在 LINQ to SQL 中,DataContext 用來管理物件的其中一種方式就是在執行查詢時,將物件識別記錄在識別快取中。 在某些情況下,LINQ to SQL 會先嘗試從識別快取擷取物件,然後再執行資料庫的查詢。
一般而言,為了讓 LINQ to SQL 查詢從識別快取傳回物件,此查詢必須以物件的主索引鍵為基礎,而且必須傳回單一物件。 特別是,此查詢必須採用下列其中一種一般格式。
注意
預先編譯的查詢不會從識別快取傳回物件。 如需預先編譯查詢的詳細資訊,請參閱 CompiledQuery 與 操作說明:儲存及重複使用查詢。
若要從識別快取擷取物件,查詢必須採用下列其中一種一般格式:
Table<TEntity>
.Function1(
predicate
)
Table<TEntity>
.Function1(
predicate
).Function2()
在這些一般格式中,Function1
、Function2
和 predicate
的定義方式如下。
Function1
可以是下列任何項目:
Function2
可以是下列任何項目:
predicate
必須是物件之主索引鍵屬性設定為常數值的運算式。 如果物件具有多個屬性所定義的主索引鍵,每個主索引鍵屬性都必須設定為常數值。 下面是 predicate
必須採用的格式範例:
c => c.PK == constant_value
c => c.PK1 == constant_value1 && c=> c.PK2 == constant_value2
範例
下列程式碼將提供 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 {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)