Skip to content

Filters: allow condition on has() function #985

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bart-degreed opened this issue May 2, 2021 · 0 comments · Fixed by #986
Closed

Filters: allow condition on has() function #985

bart-degreed opened this issue May 2, 2021 · 0 comments · Fixed by #986
Assignees

Comments

@bart-degreed
Copy link
Contributor

bart-degreed commented May 2, 2021

Proposal

Currently, the has function in query string filters takes a single parameter: the target collection. For example:

GET /users?filter=has(roles) HTTP/1.1

This returns the subset of users that are associated with at least one role. Combining this with an include, we can filter the included set of roles to a specific subset:

GET /users?filter=has(roles)&include=roles&filter[roles]=equals(name,'Administrator') HTTP/1.1

This returns the subset of users that are associated with at least one role, then filters the returned roles with name Administrator. The problem is that the resulting subset of customers now may contain an empty set of roles (in case a customer does have a role, but it is not named Administrator).

What we'd like to get instead, is the subset of customers that have at least one role with name Administrator. This proposal enables that by adding an optional condition to the has function, which allows to specify a filter. Then we can write:

GET /users?filter=has(roles,equals(name,'Administrator'))&include=roles&filter[roles]=equals(name,'Administrator') HTTP/1.1

And this returns the subset of users that are associated with at least one role named Administrator, along with only those roles. So no more customers with an empty included set of roles.

Implementation

Currently, the has function translates to the LINQ Any() extension method. An overload of Any() exists that takes a predicate. So if the has function contains the optional condition, we'll call the overload with predicate instead. EF Core translates that to a WHERE EXISTS(...) clause.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

1 participant