How do I use OData Expand like a SQL join?

99封情书 提交于 2019-12-02 20:25:17
Vitek Karas MSFT

The right way to do this would be something like:

http://odata.stackexchange.com/stackoverflow/atom/Users(1569)?$expand=Comments

The problem is that there seem to be no users in the data source (don't know why), so the above query will return a 404. But it is the right syntax.

The idea is that if you want information about just one user you "navigate" to it by using the /Users(1569) (the stuff in parethesis is the primary key of the entity set). Then if you also want to include all the comments, you simply add $expand=Comments. If you want just the comments and not the information about the user you can do /Users(1569)/Comments.

Note that the service you used doesn't define navigation properties, so the above won't work as "joins" are not really supported. But the stackexchange odata endpoint does have the navigation properties defined.

Basically the joins are defined on the server/service so that the client doesn't have to know which column is a foreign key to which primary key.

It also helps with data sources which don't use relational databases as their storage, as it doesn't force them to create fake foreign keys.

You can expand down further "layers" of the graph. If the entity returned in the expand also defines further navigation properties, then you can specify a comma-separated list of the navigation properties.

Here's an example for a made-up service, note that this is expanding each customer in the collection, which is similar to a multiple join.

.../Customers?$expand=Orders,OrderDetails

Try the .Intersect() method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!