$select Enhancement in ASP.NET Core OData

Sam Xu


The release of ASP.NET Core OData v7.3 brings a ton of improvements to $select functionality. In this article, I’d like to introduce some of the new features of $select and its usages in combination with other query options like $filter, $top, $skip, $orderby, $count and $expand.
This tutorial assumes that you already have the knowledge to build an ASP.NET Core Web Application service using ASP.NET Core OData NuGget package. If not, start by reading ASP.NET Core OData now Available and refer to the sample project used in this article.
Let’s get started.

Data Model

As mentioned, we are going to skip the steps to create an ASP.NET Core Web Application with OData functionalities enabled. However, to get a good understanding of the scenarios listed in this article, it is important for us to see the model types used in this sample project.

Below are the CLR class types used in sample project:


  • Type “Customer”, “Order”, “ZipCode” serve as Edm entity types.
  • Type “Address” and “BillAddress” serve as Edm complex types, and “BillAddress” is derived from “Address”.
  • Address” has a navigation property named “ZipCode“.

In the corresponding Edm model, I have “Customers”, “Orders” and “ZipCodes” as the Edm entity sets related to the above Edm types.

Besides, I have two real “Customers” named “Balmy” and “Chilly” in the sample project. For other properties’ information, you can refer the source code or build, run, and send a http://localhost:5000/odata/Customers to get.


$select is one of OData supported query options which allows the clients to select specific properties from the server. The biggest advantage of using $select is that the heavy lifting is done by the server before the data is returned, which leads to better performance. Hassan Habib’s Optimizing Web Applications with OData $Select shares some advantages to use $select.

For example, we can select a complex property using $select like:

We can get:

In this way, we can get data performance by limiting the result only including the properties wanted. That is, server doesn’t need to return all properties belong to “Customer(1)”, meanwhile the client doesn’t need to trim the result.

Select path in $select

The above example is a very basic usage of $select. With the release of ASP.NET Core OData v7.3.0, we now have support to use select path in $select.

For example:

In summary, the select path should follow up the following basic rules:

  • If only one segment exists, it could be “*”, “NS.*”, “Structural property” segment or “Navigation property” segment.
  • Otherwise, the last segment in a select path could be “Structural property” segment or “Navigation property” segment, and the other segments could be “Complex property” segment or “Complex type cast” segment.

For the above request, we can get the following result:

Note: The result only includes the Street in HomeAddress.

Type cast select path in $select

It also supports the type cast in the select path, for example:

We can get:


  • The first customer’s HomeAddress is not a BillAddress, so the entity for this customer only includes the HomeAddress property with empty object.
  • The second customer’s HomeAddress is a BillAddress, so it includes the selected property named FirstName and a control metadata property named @odata.type.

Nested $select in $select

We can use the nested $select to replace the above select path. In fact, It’s more understandable to use nested $select.

A simplified example should look as follows:

We can get:

Note, the context URI in this scenario is not correct. It should be same as the context URI in select path scenario. It’s a known issue and will be fixed in the future release.

Select sub navigation property in $select

It also supports to select the sub navigation property in a complex property. For example:

We can get:

You may be wondering “Why the HomeAddress is empty object here?” It’s empty not because it’s not a BillAddress, but because the navigation link control information is omitted by default in the “Minimal” metadata level. In OData, if we don’t set the metadata level, by default it’s “Minimal” metadata level. So, if we want to get all control metadata information, we can use $format to set the “Full” metadata level as below:

We can get:

Again, we can use nested $select to get the same payload result as below (except the context URI):

Selection on collection property

Now, there’s support to select path and nested select on collection complex property. For example:

We can get:

Note: The result in select path and nested select is almost same except the context URI.

Nested $filter, $top, $skip, $orderby, $count

Besides the nested $select, there is support for nested $filter, $top, $skip, $orderby and $count on collection property selection.

For example, we can select the collection property of string as:

We can get:

  • Now, we can add nested $filter as:

  • We can add nested $top, $skip as:

  • Also, we can add $orderby as:

Or order by in descending order as:

The above query options can also apply to complex type collection property, for example:

  • $filter on collection complex property

  • $top, $skip, $count, $orderby on collection complex property

Note: So far, $filter, $top, $skip and $orderby work for “all” type collection structural property, such as primitive type, Enum type and complex type. However, $count only works for complex type collection property.

Nested $expand in $select (?)

It also supports to expand the navigation property under a complex property. For example, we can get a customer with “HomeAddress” selected meanwhile “ZipCode” is included under HomeAddress property.

It seems that we can use the “nested $expand” in selection, same as nested $filter as above, like:

However, it’s not allowed, not supported by design. Even though the OData spec says that you may use “$select with nested $expand”, but there is no compelling use case for such a scenario. There’s active discussion around this topic and this might change in the near future. So, at this time, we decided not to provide support for nested $expand in $select.

However, it doesn’t mean that we cannot accomplish the above goal. We can combine $select and $expand together to get the result.

Let’s start from the expand path. Simply put, we can use the expand path to expand a navigation property under a complex property as below:

We can see “ZipCode” navigation property included under “HomeAddress”.

We can also use the same pattern on the navigation property of collection complex property as:

We can get the following result:

However, we cannot use nested $expand in this scenario as below:

A $expand query as such will get an error message stating that “FavoriteAddresses” is not a navigation property and you can’t expand on a non-navigation property.

To summarize, the expand path should follow up the following basic rules:

  • The last segment in the expand path should be navigation property segment.
  • The other segment could be complex property segment or type cast segment.

Combine $select and $expand

Even though we cannot use $expand in $select, we can combine them together to get more interesting results, for example:

We can get:

You may notice that in this scenario, I only select “Name” explicitly, without select “HomeAddress”, and only expand “ZipCode” in “HomeAddress”, the payload should only include “Name” property and “ZipCode” of “HomeAddress”. The above payload does not seem correct. It’s a known issue and will be fixed in the future release.

To get the result only including “ZipCode” of “HomeAddress”, we can construct a query like:

which gets:

Note, in “Full” metadata level, the payload includes the navigation link metadata.


Thanks for reading this article. I hope you enjoy the new $select usages released in new ASP.NET Core OData. If you have any questions, comments, concerns, please feel free send email to saxu@microsoft.com.  If you find any issues or have a feature request, please go to odata@github.

For the sample project used in this article, you can find it here. And big thanks for reviewing from Saurahb Madan.


Leave a comment