CAST (Entity SQL)

Converts an expression of one data type to another.


CAST ( expression AS data_type )  


Any valid expression that is convertible to data_type.

The target system-supplied data type. It must be a primitive (scalar) type. The data_type used depends on the query space. If a query is executed with the EntityCommand, the data type is a type defined in the conceptual model. For more information, see CSDL Specification. If a query is executed with ObjectQuery<T>, the data type is a common language runtime (CLR) type.

Return Value

Returns the same value as data_type.


The cast expression has similar semantics to the Transact-SQL CONVERT expression. The cast expression is used to convert a value of one type into a value of another type.

CAST( e as T )  

If e is of some type S, and S is convertible to T, then the above expression is a valid cast expression. T must be a primitive (scalar) type.

Values for the precision and scale facets may optionally be provided when casting to Edm.Decimal. If not explicitly provided, the default values for precision and scale are 18 and 0, respectively. Specifically, the following overloads are supported for Decimal:

  • CAST( d as Edm.Decimal );

  • CAST( d as Edm.Decimal(precision) );

  • CAST( d as Edm.Decimal(precision, scale) );

The use of a cast expression is considered an explicit conversion. Explicit conversions might truncate data or lose precision.


CAST is only supported over primitive types and enumeration member types.


The following Entity SQL query uses the CAST operator to cast an expression of one data type to another. The query is based on the AdventureWorks Sales Model. To compile and run this query, follow these steps:

  1. Follow the procedure in How to: Execute a Query that Returns PrimitiveType Results.

  2. Pass the following query as an argument to the ExecutePrimitiveTypeQuery method:

SELECT VALUE cast(p.ListPrice as Edm.Int32) 
    FROM AdventureWorksEntities.Products as p order by p.ListPrice

See also