System.TimeSpan Methods (LINQ to SQL)

Member support for System.TimeSpan greatly depends on the versions of the .NET Framework and Microsoft SQL Server that you are using.

When a method, operator, or property is unsupported; it means that LINQ to SQL cannot translate the member for execution on the SQL Server. You may still be able to 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.

Previous Limitations

When using LINQ to SQL with versions of the .NET Framework prior to .NET Framework 3.5 SP1, you cannot map SQL Server database fields to System.TimeSpan. However, operations on TimeSpan are supported because TimeSpan values can be returned from DateTime subtraction or introduced into an expression as a literal or bound variable.

Supported System.TimeSpan Method Support

The following LINQ to SQL-supported methods, operators, and properties are available for you to use in your LINQ to SQL queries. Once mapped in the object model or external mapping file, LINQ to SQL allows you to call many of the System.TimeSpan members inside your LINQ to SQL queries.

Supported TimeSpan Methods

Supported TimeSpan Operators

Supported TimeSpan Properties

Compare

Equality

Days

CompareTo(TimeSpan)

GreaterThan

Hours

Duration

GreaterThanOrEqual

MaxValue

Equals(TimeSpan, TimeSpan)

Inequality

Milliseconds

Equals(TimeSpan)

LessThan

Minutes

LessThanOrEqual

MinValue()

Note

The ability to map System.TimeSpan to a SQL TIME column with LINQ to SQL requires the .NET Framework 3.5 SP1 and beyond. The SQL TIME data type is only available in Microsoft SQL Server 2008 and beyond.

Addition and Subtraction

Although the CLR System.TimeSpan type does support addition and subtraction, the SQL TIME type does not. Because of this, your LINQ to SQL queries will generate errors if they attempt addition and subtraction when they are mapped to the SQL TIME type. You can find other considerations for working with SQL date and time types in SQL-CLR Type Mapping (LINQ to SQL).

See Also

Reference

SQL-CLR Type Mapping (LINQ to SQL)

Other Resources

Query Concepts in LINQ to SQL

Creating the Object Model (LINQ to SQL)

Data Types and Functions (LINQ to SQL)