Bahasa
Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
Topik ini menjelaskan jenis kueri LINQ ke SQL yang mengembalikan objek dari cache identitas yang dikelola oleh DataContext.
Di LINQ ke SQL, salah satu cara DataContext dalam mengelola objek adalah dengan mencatat identitas objek ke dalam cache identitas ketika kueri sedang dieksekusi. Dalam beberapa kasus, LINQ ke SQL akan mencoba mengambil objek dari cache identitas sebelum menjalankan kueri dalam database.
Secara umum, agar kueri LINQ ke SQL mengembalikan objek dari cache identitas, kueri harus didasarkan pada kunci utama objek dan harus mengembalikan satu objek. Secara khusus, kueri harus berada di salah satu formulir umum yang diperlihatkan di bawah ini.
Nota
Kueri yang telah dikompilasi sebelumnya tidak akan mengembalikan objek dari cache identitas. Untuk informasi selengkapnya tentang kueri yang telah dikompilasi sebelumnya, lihat CompiledQuery dan Cara: Menyimpan dan Menggunakan Kembali Kueri.
Kueri harus berada di salah satu formulir umum berikut untuk mengambil objek dari cache identitas:
Table<TEntity>
.Function1(predicate)Table<TEntity>
.Function1(predicate).Function2()
Dalam bentuk umum ini, Function1, Function2, dan predicate didefinisikan sebagai berikut.
Function1 dapat berupa salah satu hal berikut:
Function2 dapat berupa salah satu hal berikut:
predicate harus berupa ekspresi di mana properti kunci utama objek diatur ke nilai konstanta. Jika objek memiliki kunci primer yang ditentukan oleh lebih dari satu properti, setiap properti kunci utama harus diatur ke nilai konstanta. Berikut ini adalah contoh formulir predicate yang harus diambil:
c => c.PK == constant_valuec => c.PK1 == constant_value1 && c=> c.PK2 == constant_value2
Contoh
Kode berikut menyediakan contoh jenis LINQ ke kueri SQL yang mengambil objek dari cache identitas.
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)