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.

.Contains method generate different commands if it take its value from variable or directly

See original GitHub issue

I have a million row in my table when i search in this table by name i use contains method which take its value from variable but the generated command has execution time bigger than the one generated if i pass value directly

if i use variable (3,779ms)

 var value = "test";
            var list1 = source
               .Where(w => w.Name.Contains(value))
               .Take(10)
               .Select((x) => new
               {
                   x.Id
               }).ToList();
  • the generated command is
  • @__p_1=‘10’
  • @__value_0=‘test’
      SELECT TOP(@__p_1) [c].[Id]
      FROM [Categories] AS [c]
      WHERE ([c].[Deleted] IS NULL) AND ((@__value_0 LIKE N'') OR (CHARINDEX(@__value_0, [c].[Name]) > 0))

if i pass value directly (1,080ms)

             var list2 = source
                .Where(w => w.Name.Contains("test"))
                .Take(10)
                .Select((x) => new
                {
                    x.Id
                }).ToList();
  • the generated command is
  • @__p_0=‘10’
      SELECT TOP(@__p_0) [c].[Id]
      FROM [Categories] AS [c]
      WHERE ([c].[Deleted] IS NULL) AND ([c].[Name] LIKE N'%test%')

EF 6

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
rojicommented, Oct 30, 2022

@mo-ah-dawood the code sample you’ve provided doesn’t make any use of Contains. Please open a new issue with a full, runnable code sample and a description of the exact problem you’re running into.

1reaction
rojicommented, Mar 26, 2022

EF Core sometimes generates different SQL for parameter and literal/constant cases. In the specific example above, we can use LIKE with a literal because we can detect and escape wildcards (%, _) when compiling the query. The same currently isn’t possible to do efficiently when the pattern is a parameter; client-side parameter transformation would allow using LIKE for this case above - I think #11881 covers that (@smitpatel do you remember if we have a better issue tracking that?).

Although we’ve seen cases where LIKE is significantly faster than CHARINDEX (#7429), this case is surprising because this is Contains rather than StartsWith, and presumably searching in the middle of the string (as above) cannot use an index in any case. So I’m not sure why LIKE would perform so much better than CHARINDEX - I can at least repro this and try to understand a bit better.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Back to Basics: The PowerShell Contains Operator
The -Contains operator checks whether the test value exists or is equal to an element of the collection. In this case, the command...
Read more >
How do I view the SQL generated by the Entity Framework?
When you run a LINQ query in the results there will be an SQL tab which shows the executed SQL statement. For mySQL...
Read more >
String.Contains Method (System)
Contains (Char, StringComparison). Returns a value indicating whether a specified character occurs within this string, using the specified comparison rules.
Read more >
contains | Cypress Documentation
.contains() acts differently whether it's starting a series of commands or being chained off an existing series. When starting a series of commands:​....
Read more >
Do you generally send objects or their member variables ...
So when you create a function, you're implicitly declaring some contract with code that is calling it. "This function takes this info, and...
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