System.DateTime Methods

The following LINQ to SQL-supported methods, operators, and properties are available to use in LINQ to SQL queries. When a method, operator or property is unsupported, LINQ to SQL cannot translate the member for execution on the SQL Server. You may use these members in your code, however, they must be evaluated before the query is translated to Transact-SQL or after the results have been retrieved from the database.

Supported System.DateTime Members

Once mapped in the object model or external mapping file, LINQ to SQL allows you to call the following System.DateTime members inside LINQ to SQL queries.

Supported DateTime Methods Supported DateTime Operators Supported DateTime Properties
Add Addition Date
AddDays Equality Day
AddHours GreaterThan DayOfWeek
AddMilliseconds GreaterThanOrEqual DayOfYear
AddMinutes Inequality Hour
AddMonths LessThan Millisecond
AddSeconds LessThanOrEqual Minute
AddTicks Subtraction Month
AddYears Now
Compare Second
CompareTo(DateTime) TimeOfDay
Equals(DateTime) Today

Members Not Supported by LINQ to SQL

The following members are not supported inside LINQ to SQL queries.

IsDaylightSavingTime IsLeapYear
DaysInMonth ToBinary
ToFileTime ToFileTimeUtc
ToLongDateString ToLongTimeString
ToOADate ToShortDateString
ToShortTimeString ToUniversalTime
FromBinary UtcNow
FromFileTime FromFileTimeUtc
FromOADate GetDateTimeFormats

Method Translation Example

All methods supported by LINQ to SQL are translated to Transact-SQL before they are sent to SQL Server. For example, consider the following pattern.

(dateTime1 – dateTime2).{Days, Hours, Milliseconds, Minutes, Months, Seconds, Years}

When it is recognized, it is translated into a direct call to the SQL Server DATEDIFF function, as follows:

DATEDIFF({DatePart}, @dateTime1, @dateTime2)

SQLMethods Date and Time Methods

In addition to the methods offered by the DateTime structure, LINQ to SQL offers the methods listed in the following table from the System.Data.Linq.SqlClient.SqlMethods class for working with date and time.

DateDiffDay DateDiffMillisecond DateDiffNanosecond
DateDiffHour DateDiffMinute DateDiffSecond
DateDiffMicrosecond DateDiffMonth DateDiffYear

See Also

Query Concepts
Creating the Object Model
SQL-CLR Type Mapping
Data Types and Functions