Context Entity and Cardinality in Expressions
Each expression contained in the Expression object is evaluated within the context of a specific entity. The specific entity within which this evaluation occurs is called the context entity. Within the expression, attribute references (the AttributeRef object) can only refer to attributes contained within the current context entity or to any entity in the same inheritance hierarchy as the current entity context. Similarly, entity references (the EntityRef object) can only refer to the current context entity or one related by inheritance.
The following list is an explanation of how the context entity and expression relate:
For grouping expressions, the context entity is the base entity of the hierarchy.
For grouping detail expressions, the context entity is the entity defined by the EntityRef element within the grouping.
For measure expressions, the context entity is the base entity of the measure group.
For hierarchy filter expressions, the context entity is the base entity of the hierarchy.
For grouping filter and sort expressions, the context entity is the base entity of the hierarchy.
For sub-expressions, the context entity depends on both the containing context entity and the path.
For calculated attributes, the context entity depends on where it is used in the query.
Paths to subcomponents of the expression start in the context entity for that expression.
Each PathItem must be meaningful in the current context. For the RolePathItem element, this means that it must be a role within the context entity.
Each PathItem will change the current context entity. For the RolePathItem element, the new context entity is the target entity of the role.
In addition, a PathItem can change the cardinality of the expression's value. For RolePathItem, if the cardinality of the role is Many or OptionalMany, the expression's value is a set. Note that if the contained value is already a set, instead of a scalar, the resulting value is not a set of sets. Instead, the individual contained sets are grouped and combined into larger sets.