Like Entity Framework's Operator?

.net entity-framework linq-to-entities sql-server

Question

Our entities include string fields, and we're attempting to use the "LIKE" operator in Entity Framework, but it doesn't seem to be supported. Has anybody else attempted to do this?

This blog entry encapsulates the problem we're experiencing. We could use contains, but it only works for the simplest LIKE scenario. We can get there by combining contains, startswith, endswith, and indexof, but doing so involves translating common wildcards into Linq to Entities code.

1
86
6/23/2009 3:18:39 PM

Accepted Answer

Although it has been a while since this article was made, such a link should still be helpful. If you are already running EF 6.2.x, go to this response. If you're using EF Core 2.x, to this response

Quick version:

The SqlFunctions.PatIndex method, which is applicable to all legal text and character data types, returns the beginning position of the first instance of a pattern in a given expression, or zeros if the pattern is not found.

Objects.Data.System is the namespace. Assembly for SQL: System.Data.Entity (in System.Data.Entity.dll)

There is also some explanation in this forum post.

33
3/8/2019 5:49:07 PM

Popular Answer

I don't really know much about EF, however with LINQ to SQL, a LIKE clause is often expressed using String. Contains:

where entity.Name.Contains("xyz")

is equivalent to

WHERE Name LIKE '%xyz%'

(Use StartsWith and EndsWith for different actions.)

Because I don't understand what you mean when you state that you're attempting to implement LIKE, I'm not completely sure whether that is helpful. Please let me know if I entirely misunderstood, and I'll remove this response.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow