C
C#16mo ago
M B V R K

✅ EF.Functions.Like() produces an exception

I'm working with EF Core with PostgreSQL, I have the following Asp.Net Core's Action:
public async Task<IActionResult> SearchByNickNames(string value)
{
var authors = await _context.Authors
.Where(a=> a.Nicknames.Any(nick => EF.Functions.Like(nick, "%ti")))
.Select(a => new
{
a.Id,
a.Name,
SocialMedia = new
{
a.SocialMedia.Facebook,
a.SocialMedia.Twitter,
a.SocialMedia.Instagram,
a.SocialMedia.LinkedIn
},
Nicknames = string.Join(", ", a.Nicknames)
}).ToListAsync();

return Ok(authors);
}
public async Task<IActionResult> SearchByNickNames(string value)
{
var authors = await _context.Authors
.Where(a=> a.Nicknames.Any(nick => EF.Functions.Like(nick, "%ti")))
.Select(a => new
{
a.Id,
a.Name,
SocialMedia = new
{
a.SocialMedia.Facebook,
a.SocialMedia.Twitter,
a.SocialMedia.Instagram,
a.SocialMedia.LinkedIn
},
Nicknames = string.Join(", ", a.Nicknames)
}).ToListAsync();

return Ok(authors);
}
After trying to test that action I get this exception: System.InvalidOperationException: The LINQ expression 'nick => __Functions_0 .Like( matchExpression: nick, pattern: "%ti")' 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 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'.
Please do you have any idea about how to fix this issue without using AsEnumerable() ? Massive thanks in advance <3
8 Replies
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
M B V R K
M B V R KOP16mo ago
DbFunctionsExtensions.Like Method (Microsoft.EntityFrameworkCore)
An implementation of the SQL LIKE operation. On relational databases this is usually directly translated to SQL.
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
M B V R K
M B V R KOP16mo ago
I fixed it using StartsWith() extension method for string
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
M B V R K
M B V R KOP16mo ago
Yeah, I think the issue was because of the nested using of the EF.Functions.Like() inside the Any() function, but it works fine when it used inside a Where without nested function inside the same Where
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
M B V R K
M B V R KOP16mo ago
Yeah that was right Also the NickNames property is an Array in the PostgreSQL and not an entity maybe this is another cause of this issue
Want results from more Discord servers?
Add your server