Search code examples
c#.net-coreodata

OData filter ids from a list of strings


I am trying to use Azure Search and OData to filter some IDs in a .net core project.

The IDs are of type Guid and the list I am trying to filter is of type List<string>.

What I am trying to accomplish is to return all the ids that are included in the List<string>

I've tried below, but it doesn't work.

Filter = SearchFilter.Create($"id contains {ids}")

Here is my function.

public SearchOptions GetSearchOptions(List<string> ids)
{
      var searchOptions = new SearchhOptions()
      {
            Filter = SearchFilter.Create($"id contains {ids}")
      }
      return searchOptions
}

Solution

  • To filter a list of GUID IDs in Azure Search using OData, you can use the in operator to check if the id field is within the list of GUIDs:

    var searchOptions = new SearchOptions
    {
        Filter = string.Join(" or ", ids.Select(id => $"id eq '{id}'"))
    };