KEY (Entity SQL)

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




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  


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.

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

SELECT VALUE Key(CreateRef(AdventureWorksEntities.Products, 
    row(p.ProductID))) FROM AdventureWorksEntities.Products AS p

See also