Database Reference
In-Depth Information
Here's the best practice. If there is a translation available, use it. It makes the code easier to read. If there is no
translation available, use the EntityFunction class to call the canonical function explicitly, as in the following code
snippet:
var laterentals = from r in context.MovieRentals
where (r.ReturnedDate - r.RentalDate).Days > 10
select r;
does not translate to the Canonical Function, so you should use,
var laterentals = from r in context.MovieRentals
where EntityFunctions.DiffDays(r.RentalDate,
r.ReturnedDate) > 10
select r;
11-10. Calling Database Functions in eSQL
Problem
You want to call a database function in an eSQL statement.
Solution
Let's say that you have an eCommerce website, and you need to find all of the customers within a certain distance of a
given ZIP code. Your model might look like the one shown in Figure 11-10 .
Figure 11-10. WebCustomer and Zip entities in a model
We'll need to pull out some basic math functions to get this to work. Unfortunately, Entity Framework does not
have the canonical functions we need, so we'll have to use the functions available in the data store.
Use the pattern in Listing 11-17 to call the database functions from an eSQL expression.
Listing 11-17. Using Database Functions to Determine the Distance between a Customer and a Given Zip Code
class Program
{
static void Main(string[] args)
{
RunExample();
}
 
Search WWH ::




Custom Search