dm k
dm k

Reputation: 131

like query in Entity framework 6

I have Entity Framework with PostgreSQL Now I do something like this:

string query = "select * from dbo.Products where name ilike '%test%phrase%' 
                or code ilike '%test%phrase%' or code2 ilike '%test%phrase%'";

var result = context.Products.SqlQuery(query);

So I get products with name like this:

test some phrase
some test
some phrase and so on.

How to perform same query with linq or other type of query without raw sql?

I need part of phrase from start of string + part from middle, mayby part from end. Something like regular expression. In postgres it can be done with % symbol

Upvotes: 0

Views: 1636

Answers (2)

Adam Benson
Adam Benson

Reputation: 8622

My original answer was wrong (my apologies to the OP.)

You can do Name.StartsWith, Name.EndsWith, or Name.Contains.

On top of that you can use Devart.Data.PostgreSql.Entity.PgSqlFunctions to achieve LIKE comparisons. i.e.

where PgSqlFunctions.Like(PgSqlFunctions.Lower(c.Name), PgSqlFunctions.Lower(pattern))

Regards,

AB

PS: If you have a lot of data that needs searching like that check out trigram indexes - https://www.postgresql.org/docs/9.6/static/pgtrgm.html

Upvotes: 1

Eldho
Eldho

Reputation: 8301

You could try this.

var result = context.Products.Where(h=>h.Name.Contains("TestPhrase") ||
                                    h.Code.Contains("TestPhrase"))
                             .ToList();

Upvotes: 0

Related Questions