Data Access has been discontinued. Please refer to this page for more information.

How to: Return a Specific Object Using its Key

This topic shows how to retrieve a data type by its key values instead of explicitly creating and executing a query. This example uses the TryGetObject<T> method on OpenAccessContext to return an object with the specified key into the object context.

To run the code in this example, you need a fluent model based on the Northwind database.

The following code is the LINQ example.

using (FluentModel dbContext = new FluentModel())
{
   Telerik.OpenAccess.ObjectKey objectKey = new Telerik.OpenAccess.ObjectKey("Customer", 
       new[] { new Telerik.OpenAccess.ObjectKeyMember("CustomerID", "ALFKI") });
   Customer customer = null;
   if (dbContext.TryGetObjectByKey<Customer>(objectKey, out customer))
   {
       Console.WriteLine(customer.CompanyName);
   }
}
Using dbContext As New FluentModel()
 Dim objectKey As New Telerik.OpenAccess.ObjectKey("Customer", 
    { New Telerik.OpenAccess.ObjectKeyMember("CustomerID", "ALFKI") })
 Dim customer_Renamed As Customer = Nothing
 If dbContext.TryGetObjectByKey(Of Customer)(objectKey, customer_Renamed) Then
  Console.WriteLine(customer_Renamed.CompanyName)
 End If
End Using

For more information about the ObjectKey API, please refer to the following topics:

See Also