Skip to content

The LINQ expression could not be translated with OData and PostgreSQL: matchesPattern filter #2818

@derek-wong12

Description

@derek-wong12

Odata Version: 8.2.0
PostgreSQL Version: 13

When using a filter like $filter=matchesPattern(LocationName,‘^.D.$’) to filter a column of type “character varying(100)” in an endpoint that uses ODataQueryOptions.ApplyTo it will always fail with error:

The LINQ expression 'DbSet<StoreLocations>()
    .Where(r => Regex.IsMatch(
        input: r.LocationName, 
        pattern: __TypedProperty_0, 
        options: ECMAScript))' could not be translated. Additional information: Translation of method 'System.Text.RegularExpressions.Regex.IsMatch' failed. If this method can be mapped to your custom function, see https://go.microsoft.com/fwlink/?linkid=2132413 for more information. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
   at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.<VisitMethodCall>g__CheckTranslated|15_0(ShapedQueryExpression translated, <>c__DisplayClass15_0& )
   at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)

PostgreSql Table:

CREATE COLLATION IF NOT EXISTS collation_case_insensitive (provider = icu, locale = 'und-u-ks-level2', deterministic = false);
CREATE TABLE IF NOT EXISTS public."StoreLocations"
        (
          "LocationName" character varying(100) COLLATE "collation_case_insensitive"
        )

C#:

public Task<IQueryable<StoreLocations>> GetAllLocations(ODataQueryOptions options)
       {
           return Task.FromResult(options.ApplyTo(_dbContext. StoreLocations.AsNoTracking(), ignoreQueryOptions).Cast<StoreLocations >());
       }

Request URL:
https://localhost:8443/storelocationssearch/$query

Payload:

{
    "$FILTER":"matchesPattern(LocationName,‘^.*D.*$’)"
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions