Magendran V
Magendran V

Reputation: 1417

OData query to filter objects based on multiple Ids

Consider that api response without any filters as below,

[
        {
            "Account": {
                "AccountID": "1",
                "Name": "Acc1"

            }
        },      
        {
            "Account": {
                "AccountID": "2",
                "Name": "Acc2"

            }
        },              
        {
            "Account": {
                "AccountID": "3",
                "Name": "Acc3"

            }
        },
        {
            "Account": {
                "AccountID": "4",
                "Name": "Acc4"

            }
        }
        ...
        ...
]       

Im using odata query to filter accounts as below, when I have array of account ids.

.......apiurl?$filter=Account/AccountID eq '1' OR Account/AccountID eq  '2' OR Account/AccountID eq '3'

This seems working. But when the account ids array has more values then the uqi query length gets increase.

Is there any other option to use the field only once Account/AccountID in uri to check against array of Ids.

Thanks in advance.

Upvotes: 1

Views: 3392

Answers (1)

Matija Lazar
Matija Lazar

Reputation: 53

When you are trying to filter for same property but different value, it is good practice to use in.

In your case, this would look like

.......apiurl?$filter=Account/AccountID in ('1', '2', '3')

Please pay attention also on queries towards Database. If you have queries like WHERE AccountId = '1' OR AccountId = '2' to switch them to WHERE AccountId IN ('1', '2') on server side - just suggestion for optimization.

Upvotes: 3

Related Questions