Database functions (DbFunctions
) are the database equivalent of C# methods. A database function can be invoked with or without parameters, and it computes the result based on the parameter values.
Most databases, which use SQL for querying have support for database functions. So SQL generated by EF Core query translation also allows invoking database functions. C# methods don't have to translate strictly to database functions in EF Core.
EF Core supports the following different ways of mapping between C# functions and database functions.
By default, EF Core providers provide mappings for various built-in functions over primitive types. For example, String.ToLower()
translates to LOWER in SqlServer. This functionality allows users to write queries in LINQ seamlessly.
String.Length
translates to LEN
in SqlServer.Since not all database functions have equivalent C# functions, EF Core providers have special C# methods to invoke certain database functions.
EF.Functions
to be used in LINQ queries.Apart from mappings provided by EF Core providers, users can also define the custom mapping.
The following are the most commonly used database functions.
Function | Description |
---|---|
Contains | A DbFunction method stub that can be used in LINQ queries to target the SQL Server CONTAINS store function. |
DataLength | Returns the number of bytes used to represent any expression. |
DateDiffDay | Counts the number of day boundaries crossed between the startDate and endDate . Corresponds to SQL Server's DATEDIFF(DAY,startDate,endDate) . |
DateDiffHour | Counts the number of hour boundaries crossed between the startDate and endDate . Corresponds to SQL Server's DATEDIFF(HOUR,startDate,endDate) . |
DateDiffMicrosecond | Counts the number of microsecond boundaries crossed between the startDate and endDate . Corresponds to SQL Server's DATEDIFF(MICROSECOND,startDate,endDate) . |
DateDiffMillisecond | Counts the number of millisecond boundaries crossed between the startDate and endDate . Corresponds to SQL Server's DATEDIFF(MILLISECOND,startDate,endDate) . |
DateDiffMinute | Counts the number of minute boundaries crossed between the startDate and endDate . Corresponds to SQL Server's DATEDIFF(MINUTE,startDate,endDate) . |
DateDiffMonth | Counts the number of month boundaries crossed between the startDate and endDate . Corresponds to SQL Server's DATEDIFF(MONTH,startDate,endDate) . |
DateDiffNanosecond | Counts the number of nanosecond boundaries crossed between the startDate and endDate . Corresponds to SQL Server's DATEDIFF(NANOSECOND,startDate,endDate) . |
DateDiffSecond | Counts the number of second boundaries crossed between the startDate and endDate . Corresponds to SQL Server's DATEDIFF(SECOND,startDate,endDate) . |
DateDiffWeek | Counts the number of week boundaries crossed between the startDate and endDate . Corresponds to SQL Server's DATEDIFF(WEEK,startDate,endDate) . |
DateDiffYear | Counts the number of year boundaries crossed between the startDate and endDate . Corresponds to SQL Server's DATEDIFF(YEAR,startDate,endDate) . |
DateFromParts | Initializes a new instance of the DateTime structure to the specified year , month , day . Corresponds to the SQL Server's DATEFROMPARTS(year, month, day) . |
DateTime2FromParts | Initializes a new instance of the DateTime structure to the specified year , month , day , hour , minute , second , fractions , and precision . Corresponds to the SQL Server's DATETIME2FROMPARTS (year, month, day, hour, minute, seconds, fractions, precision) . |
DateTimeFromParts | Initializes a new instance of the DateTime structure to the specified year , month , day , hour , minute , second , and millisecond . Corresponds to the SQL Server's DATETIMEFROMPARTS(year, month, day, hour, minute, second, millisecond) . |
DateTimeOffsetFromParts | Initializes a new instance of the DateTimeOffset structure to the specified year , month , day , hour , minute , second , fractions , hourOffset , minuteOffset , and precision . Corresponds to the SQL Server's DATETIMEOFFSETFROMPARTS (year, month, day, hour, minute, seconds, fractions, hour_offset, minute_offset, precision) . |
FreeText | A DbFunction method stub that can be used in LINQ queries to target the SQL Server FREETEXT store function. |
IsDate | Validate if the given string is a valid date. Corresponds to the SQL Server's ISDATE('date') . |
Like | An implementation of the SQL LIKE operation. On relational databases, this is usually directly translated to SQL. |
SmallDateTimeFromParts | Initializes a new instance of the DateTime structure to the specified year , month , day , hour , and minute . Corresponds to the SQL Server's SMALLDATETIMEFROMPARTS (year, month, day, hour, minute) . |
TimeFromParts | Initializes a new instance of the TimeSpan structure to the specified hour , minute , second , fractions , and precision . Corresponds to the SQL Server's TIMEFROMPARTS (hour, minute, seconds, fractions, precision) . |
Let's have a look at the following example.
using (var context = new BookStore())
{
var list = context.Authors
.Include(a => a.Books)
.Where(a => EF.Functions.Like(a.LastName, "Al%"))
.ToList();
foreach (var author in list)
{
Console.WriteLine(author.FirstName + " " + author.LastName);
foreach (var book in author.Books)
{
Console.WriteLine("\t" + book.Title);
}
}
}
The above code will retrieve only those authors which have the LastName
starts with "Al".