16

In the Full .Net EF LIKE's used to be SqlMethods.Like, eg:

from c in dc.Organization
where SqlMethods.Like(c.Boss, "%Jeremy%")

This doesn't work in EF Core and I get this error:

The name SqlMethods does not exist in the current context.

So how do you do a LIKE using Entity Framework CORE?

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321

3 Answers3

35

The LIKE function has moved under EF.Functions in Core:

from c in dc.Organization
where EF.Functions.Like(c.Boss, "%Jeremy%")
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
2

Fluent version:

dc.Organization.Where(o => EF.Functions.Like(o.Boss, "%Jeremy%"));
Shenron
  • 420
  • 4
  • 11
1

Instead of Like function you can use Contains

from c in dc.Organization
where c.Boss.Contains("Jeremy") 
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
Kaval Patel
  • 670
  • 4
  • 20
  • 2
    No, if you run say SQL profiler to capture the raw SQL query you'll see `Contains` uses SQLs `CHARINDEX` Functions. `EF.Functions.Like` maps correctly to SQL `LIKE` in the query. – Jeremy Thompson Nov 14 '17 at 06:15
  • but when a huge amount of data is there then use CHARINDEX it's work faster then like – Kaval Patel Nov 14 '17 at 06:25
  • It's not the same, say for example when Full-Text Search is enabled. – Jeremy Thompson Nov 14 '17 at 06:26
  • 4
    Also when the search term is null, CHARINDEX returns nothing, while LIKE returns everything. – Jeremy Thompson Nov 14 '17 at 06:27
  • yeh! that's true – Kaval Patel Nov 14 '17 at 06:28
  • Contains can be used _in this example_ but in general it is much less than Like. Even MS SQL pattern matching is much more, e.g. "_________2___" is a like expression but not a Contains expression. (Okay, in this simple case you can use charindex.) It's not a theoretical thing, in Scala (a pretty old ERP) all ledger dimensions are merge into a big string and one must make filter by positional values. – Peter Krassoi Oct 17 '19 at 13:30