如何像 SQL 连接一样使用 OData Expand?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3920758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I use OData Expand like a SQL join?
提问by Factor Mystic
I'm trying to figure out how to accomplish the equivalent of:
我试图弄清楚如何完成相当于:
select *
from Users u
inner join Comments c on c.UserId = u.Id
where Id = 1569
(table aliases for better sql readability)
(表别名更好的 sql 可读性)
...on the StackOverflow OData endpoint. How would this url be constructed? I'm looking at the documentation for Expandat OData.org and I would have thought it'd look something like:
...在 StackOverflow OData 端点上。这个 url 将如何构建?我正在查看OData.org 上的Expand文档,我认为它看起来像:
https://odata.sqlazurelabs.com/OData.svc/v0.1/rp1uiewita/StackOverflow/Users?$Expand=Comments&$filter=UserId eq 1569
but isn't right.
https://odata.sqlazurelabs.com/OData.svc/v0.1/rp1uiewita/StackOverflow/Users?$Expand=Comments&$filter=UserId eq 1569
但不对。
In Linq, it would be this (I think), but Join isn't supported:
在 Linq 中,应该是这样(我认为),但不支持 Join:
Users.Where(u=>u.Id==1569).Join(Comments, u=>u.Id, c=>c.UserId, (a,b)=>a.Id==b.UserId)
Users.Where(u=>u.Id==1569).Join(Comments, u=>u.Id, c=>c.UserId, (a,b)=>a.Id==b.UserId)
I don't need to figure this out in Linq strictly, I'm just trying to figure out how to construct the query url. Basically, how can I translate the SQL join predicate to an OData url and do this in one call?
我不需要在 Linq 中严格弄清楚这一点,我只是想弄清楚如何构造查询 url。基本上,如何将 SQL 连接谓词转换为 OData url 并在一次调用中执行此操作?
采纳答案by 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.
问题是数据源中似乎没有用户(不知道为什么),所以上面的查询会返回404。但它是正确的语法。
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
.
这个想法是,如果您只需要有关一个用户的信息,您可以使用/Users(1569)
(括号中的内容是实体集的主键)“导航”到它。然后,如果您还想包含所有评论,只需添加$expand=Comments
. 如果您只想要评论而不是有关用户的信息,您可以这样做/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.
请注意,您使用的服务没有定义导航属性,因此上述内容不起作用,因为“连接”不受真正支持。但是 stackexchange odata 端点确实定义了导航属性。
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
回答by Joel Coehoorn
Try the .Intersect()method.
尝试.Intersect()方法。