KEY (Entity SQL)

Extracts the key of a reference or of an entity expression.

KEY(createref_expression)

Remarks

An entity key contains the key values in the correct order of the specified entity or entity reference. Because multiple entity sets can be based on the same type, the same key might appear in each entity set. To get a unique reference, use REF. The return type of the KEY operator is a row type that includes one field for each key of the entity, in the same order.

In the following example, the key operator is passed a reference to the BadOrder entity, and returns the key portion of that reference. In this case, a record type with exactly one field corresponding to the Id property.

select Key( CreateRef(LOB.BadOrders, row(o.Id)) ) 
from LOB.Orders as o

Example

The following Entity SQL query uses the KEY operator to extract the key portion of an expression with type reference. 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 StructuralType Results (EntityClient).

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

SELECT VALUE Key(CreateRef(AdventureWorksEntities.Product, 
    row(p.ProductID))) FROM AdventureWorksEntities.Product as p

The following is the output:

ProductID: 980
ProductID: 365
ProductID: 771
ProductID: 404
ProductID: 977
...

See Also

Reference

CREATEREF (Entity SQL)
REF (Entity SQL)
DEREF (Entity SQL)

Concepts

Entity SQL Reference

Other Resources

Reference Operators