Robust ISearchBinder for OData v9: case-insensitive substring across strings, numeric substring (e.g., "30" matches 300), booleans, and DateTime (with EF-friendly year/date fallbacks). Designed to compose on IQueryable and execute in the DB with EF Core.
$ dotnet add package BerrySoftwareHQ.OData.GenericSearchBinderA lightweight and robust ISearchBinder implementation for ASP.NET Core OData ($search). It builds a boolean predicate that performs a safe, case‑insensitive Contains search across an entity’s readable scalar properties.
Works with .NET 8 and Microsoft.AspNetCore.OData v8+.
using BerrySoftwareHQ.OData.GenericSearchBinder;
using Microsoft.AspNetCore.OData;
using Microsoft.AspNetCore.OData.Query.Expressions;
builder.Services.AddControllers().AddOData(opt =>
{
opt.AddRouteComponents("odata", GetEdmModel())
.Count()
.Filter()
.OrderBy()
.Select()
.Expand()
.SetMaxTop(100)
.EnableQueryFeatures();
});
// Register the custom $search binder
builder.Services.AddSingleton<ISearchBinder, GenericSearchBinder.GenericSearchBinder>();
GET /odata/Products?$search=laptop
GET /odata/Orders?$search=2024 AND shipped
GET /odata/People?$search=true // will match boolean properties equal to true
For a given entity type, the binder builds an Expression<Func<TEntity,bool>> that evaluates to true if any readable scalar property matches the search term(s). Logical operators AND/OR/NOT are supported as provided by OData $search.
Notes on translation:
Licensed under the MIT License.