0
votes

I want to create an SQL query with EFCore like below.

FROM "Customers" AS c
            where reverse("Phone") LIKE reverse('%#####');
      ORDER BY 1
      LIMIT @__p_2 OFFSET @__p_1

I'm trying do with LINQ but this code is not working and also I couldn't find reverse method inside EF.Functions

query = query.Where(c => c.Phone.Reverse().ToString() == $"{phone}%");

throws an error

Message": "The LINQ expression 'DbSet\r\n .Where(c => c.Phone\r\n .Reverse().ToString() == __Format_0)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

Is there any way to create reverse function query with PostgreSQL/Npgsql provider for Entity Framework Core.

1

1 Answers

1
votes

Unfortunately there's no simple/idiomatic way to express string reversal in .NET; note that c.Phone.Reverse().ToString() does not return the reversed phone, since it invokes ToString on the IEnumerable returned from Reverse.

I've added EF.Functions.Reverse for the upcoming version 5.0 - see https://github.com/npgsql/efcore.pg/pull/1496.