Home > Blockchain >  Trying to compare the two strings inside the linq query
Trying to compare the two strings inside the linq query

Time:09-22

I am trying to check the equality on two strings using the EF Functions like a method, but it is failing somehow and getting null on spaces. The two strings are like as below, Here you can observe the only difference is the case for SPACE

the displayName is L1-008-5 SPACE and I have stored Displayname in space identity object as like this L1-008-5 Space

L1-008-5 SPACE and L1-008-5 Space

Here is the code

 var space = dbContext.Spaces.SingleOrDefault(a => EF.Functions.Like(a.SpaceIdentity.DisplayName, displayName));

and I tried the below options as well

dbContext.Spaces.SingleOrDefault(s => s.SpaceIdentity.DisplayName.Equals(displayName, StringComparison.OrdinalIgnoreCase));

dbContext.Spaces.SingleOrDefault(s => string.Equals(s.SpaceIdentity.DisplayName,displayName, StringComparison.OrdinalIgnoreCase));

None of the above are working and getting null on spaces.

Could anyone please point me in the right direction where I am doing wrong with the above comparison.

Many thanks in advance!

CodePudding user response:

Assuming you are using the Npgsql.EntityFrameworkCore.PostgreSQL provider for EF Core, you should have access to the ILike method which is a case-insensitive LIKE. That means you are able to use this code:

var space = dbContext.Spaces
    .SingleOrDefault(a => 
        EF.Functions.ILike(a.SpaceIdentity.DisplayName, displayName));
//                   ^^^^^
  • Related