Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq Query Where() SQL % equivalent

Tags:

c#

sql

linq

We can use the method .Contains(string) in a LINQ Expression, that sounds like '%search text%', method .StartsWith(string), that sounds like 'search text%', and method .EndsWith(string), that sounds like '%search text'.

But I need something that sounds '%search%text%', that finds all content containing 'search' and 'text', but not sequential.

Example: I have these records:

search my text

search for the text

seek the text

In SQL, the query with LIKE '%search%text%' brings:

search my text

search for the text

But not brings the 'seek the text'.

Any ideas?

like image 258
Matheus Gelinski Avatar asked Apr 16 '15 22:04

Matheus Gelinski


1 Answers

You can use one of the helper method:

var result = from o in ctx.table
where SqlMethods.Like(o.column, "%search%text%")
select o.column;
like image 183
Giorgi Nakeuri Avatar answered Oct 09 '22 23:10

Giorgi Nakeuri