Database Reference
In-Depth Information
context.MovieRentals.Add(mr1);
context.MovieRentals.Add(mr2);
context.MovieRentals.Add(mr3);
context.SaveChanges();
}
using (var context = new EFRecipesEntities())
{
Console.WriteLine("Movie rentals late returns");
Console.WriteLine("==========================");
var late = from r in context.MovieRentals
where DbFunctions.DiffDays(r.RentalDate, r.ReturnedDate) > 10
select r;
foreach (var rental in late)
{
Console.WriteLine("{0} was {1} days late, fee: {2}", rental.Title,
(rental.ReturnedDate - rental.RentalDate).Days - 10,
rental.LateFees.ToString("C"));
}
}
}
}
The output of the code in Listing 11-16 is the following:
Movie rentals late returns
==========================
A Day in the Life was 3 days late, fee: $3.00
Jim's Story was 7 days late, fee: $3.00
How It Works
Canonical functions, which are defined in Entity Framework, are data source-agnostic and supported by all data
providers. The types returned from canonical functions are defined in terms of types from the Entity Data Model.
In this recipe, we used the DiffDays() function to calculate the number of days between the start and end of the
rental period. Because DiffDays() is a canonical function, it will be implemented by all providers.
Best Practice
You may be asking yourself, “When should I use EntityFunctions?” Entity Framework provides translations for some
expressions into the canonical functions, but the translation is limited. Not every CLR method will translate to the
corresponding canonical function.
 
Search WWH ::




Custom Search