question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot translate Contains for simple string comparison (not a collection)

See original GitHub issue

After upgrading to 3.0 method using Where with string.Contains(string) inside throws translation error.

Steps to reproduce


 Context.Entity.Where(x => x.Value.ToLower().Contains(searchFilter))
                       .AsQueryable();

x is an object with properties int Id and string Value. searchFilter is a simple string.

Exception similiar to the issue when Contains is called on Collections (saw this issue already reported):

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()

Of course it was working fine before upgrading to 3.0. So is there a way to write it as translateable to SQL or do I have to call the query without this filter and then filter it locally?

Further technical details

EF Core version: 3.0 Database provider: Microsoft.EntityFrameworkCore.SqlServer (MSSQL 2016) Target framework: .NET Core 3.0 Operating system: IDE: Visual Studio 2019 16.3.7

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
rojicommented, Nov 5, 2019

@MichalLechowski as we made changes in this area (#18486), can you please try with 3.1.0-preview2 and report the results?

Regardless, I couldn’t repro the issue on 3.0 with the following code. If your issue persists with 3.1.0-preview2, could you please submit a full repro such as the following?

class Program
{
    static void Main(string[] args)
    {
        using var ctx = new BlogContext();
        ctx.Database.EnsureDeleted();
        ctx.Database.EnsureCreated();

        var searchFilter = "f";
        var result = ctx.SomeEntities
            .Where(x => x.Value.ToLower().Contains(searchFilter))
            .ToList();
    }
}

public class BlogContext : DbContext
{
    public DbSet<SomeEntity> SomeEntities { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder.UseSqlServer(...);
}

public class SomeEntity
{
    public int Id { get; set; }
    public string Value { get; set; }
}

This produces the following SQL (again on 3.0):

SELECT [s].[Id], [s].[Value]
FROM [SomeEntities] AS [s]
WHERE ((@__searchFilter_0 = N'') AND @__searchFilter_0 IS NOT NULL) OR (CHARINDEX(@__searchFilter_0, LOWER([s].[Value])) > 0)
0reactions
ajcvickerscommented, Nov 15, 2019

EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.

BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we’d like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Net core EF 3.1 LINQ string comparison no longer working
After it is converted to Net Core EF 3.1, there is an error. LINQ expression could not be translated. Either rewrite the query...
Read more >
Best Practices for Comparing Strings in .NET
Compare and String.CompareTo methods to sort strings, not to check for equality. Use culture-sensitive formatting to display non-string data, ...
Read more >
String Matching in LINQ
The simple string parameter is translated directly into a SQL string parameter. The strings are compared in full length using the string ......
Read more >
Programming in Objective-C - Page 318 - Google Books Result
An actual copy of the string contents is made here, not just another ... a simple assignment like this: res = str1; As...
Read more >
Asiatic Journal and Monthly Register for British and Foreign ...
As far as our comparison has gone , and that has not been inconsiderable , it is a ... But we cannot dismiss...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found