Filtering a 2-level hierarchy with contains in ODATA-v4

时间:2019-01-18 18:48:04

标签: odata odata-v4

Assuming the relationship between Race, Team and Car Manufacturer are all many:many. A race can feature many teams, a team can have many car manufacturers, car manufacturers can sponsor many teams and teams can enter many races.

Using odata v-4, how can I select all races featuring cars by specified manufacturers.

If I wanted to select all the races including Team with id 475 and 476 I would form my odata query as

Race$expand=Team($select=id,name)&$filter=((Team/any(c:((c/id eq 475) or (c/id eq 476)))))

But how would I form my URL if I wanted to select all races featuring teams that use car manufacturer with ford or chevy.

IN SQL I would just do:

SELECT *
FROM   race
WHERE  id IN ((SELECT raceid
               FROM   race_team
               WHERE  teamid IN (SELECT teamid
                                 FROM   team_carmanufacturer
                                 WHERE  carid IN (SELECT id
                                                  FROM   carmanufacturer
                                                  WHERE name IN
                                                 ( 'ford', 'chevy' )
                                                 )))) 

race_team, team_carmanufacturer are just the many to many mapping tables in the database.

enter image description here

1 个答案:

答案 0 :(得分:2)

您可以尝试这样的事情:

Race?$filter=Team/any(y:y/Manufacturer/any(z:z/name eq 'ford' or z/name eq 'chevy' ))&$expand=Team($expand=Manufacturer)

在没有端点的情况下很难正确地做到这一点。 另外,请确保您的Controller支持的扩展深度至少为 2

TBH,我认为随着时间的流逝,这不是一种非常实用的方法。考虑使用静态端点,该端点在您的制造商品牌上作为参数运行。

欢呼