Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include space in EF Core Startswith

When doing an EF Core query with StartsWith, then space is not included e.g. this query (notice space after Bob):

var query = _context.Persons
                    .Where(p => p.FullName.StartsWith("Bob ")
                    .Select(p => p.FullName);

will include Bobby, Bobfish and Bobwhatever because the SQL uses LEN('Bob ') that is 3 because LEN does not include trailing spaces.

What is the trick to include spaces?

like image 609
Thomas Koelle Avatar asked Sep 12 '25 23:09

Thomas Koelle


1 Answers

You can try using LIKE explicitly via EF.Functions:

var query = _context.Persons
    .Where(p => EF.Functions.Like(p.FullName, "Bob %"))
    .Select(p => p.FullName);
like image 62
Guru Stron Avatar answered Sep 15 '25 14:09

Guru Stron