In OData, the $expand
query option is used to retrieve related entities along with the requested entities in a single round-trip to the OData service.
The basic syntax for using $expand
is as follows:
bashhttp://serviceRootURL/EntitySet?$expand=NavigationProperty1,NavigationProperty2...
where serviceRootURL
is the base URL of the OData service, EntitySet
is the name of the entity set you want to query, and NavigationProperty1
, NavigationProperty2
, etc. are the names of the navigation properties that represent the related entities you want to retrieve.
For example, let's say you have an OData service that exposes a collection of Orders
, each of which has an associated Customer
entity through a navigation property called Customer
. If you want to retrieve all the orders in the collection along with the associated customer entities, you can use the following query:
bashhttp://serviceRootURL/Orders?$expand=Customer
This will return a response that includes all the orders in the collection, with the associated customer entities embedded within each order entity.
Note that you can also use $expand
to retrieve related entities at multiple levels of nesting by chaining navigation properties together. For example, if you have an Order
entity that has an associated Customer
entity, and the Customer
entity in turn has associated Address
and CreditCard
entities, you can use the following query to retrieve all the orders along with their associated customer, address, and credit card entities:
bashhttp://serviceRootURL/Orders?$expand=Customer($expand=Address,CreditCard)
This will return a response that includes all the orders in the collection, with the associated customer entities embedded within each order entity, and the associated address and credit card entities embedded within each customer entity.
Comments
Post a Comment