Database Reference
In-Depth Information
using (var context = new EFRecipesEntities())
{
var apps = from a in context.Appointments
where SqlFunctions.DatePart("WEEKDAY", a.StartsAt) == 4
select a;
Console.WriteLine("Appointments for Thursday");
Console.WriteLine("=========================");
foreach (var appointment in apps)
{
Console.WriteLine("Appointment from {0} to {1}",
appointment.StartsAt.ToShortTimeString(),
appointment.GoesTo.ToShortTimeString());
}
}
}
}
The output of the code in Listing 11-18 is as follows:
Appointments for Thursday
=========================
Appointment from 9:00 AM to 11:00 AM
Appointment from 1:00 PM to 3:00 PM
How It Works
Database functions are available for use in both eSQL and LINQ queries. These functions are exposed via methods
in the SqlFunctions class. Because these functions execute on the database side, the behavior you get might differ
slightly from what you would expect on the .NET side. For example, DayOfWeek.Thursday evaluates to 4 on the .NET
side. On the database side, Thursday is the fifth day of the week, so we check for a value of 5.
As with database functions in eSQL, not all database functions are available for LINQ queries. Check the current
documentation from Microsoft for a complete list of the available functions.
11-12. Defining Built-in Functions
Problem
You want to define a built-in function for use in an eSQL or LINQ query.
Solution
Let's say that you want to use the IsNull function in the database, but this function is not currently exposed by Entity
Framework for either eSQL or LINQ. Suppose we have a WebProduct entity in our model like the one shown in
Figure 11-12 .
 
 
Search WWH ::




Custom Search