Skip to content

Filters: allow condition on has() function #985

Closed
@bart-degreed

Description

@bart-degreed

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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions