like query in Entity framework 6

entity-framework-6 linq postgresql

Question

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

1
0
2/15/2017 9:45:29 AM

Accepted Answer

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

1
5/31/2017 10:30:56 AM

Popular Answer

You could try this.

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


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