Linq Query Where () SQL% equivalent

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

But I need something that sounds β€œ% search% text%”, which finds all the content containing β€œsearch” and β€œtext” but not sequential.

Example: I have these entries:

search text

text search

search text

In SQL, a query with LIKE '%search%text%' yields:

search my text

search for text

But it does not bring 'seek the text' .

Any ideas?

+6
source share
2 answers

You can use one of the helper methods:

 var result = from o in ctx.table where SqlMethods.Like(o.column, "%search%text%") select o.column; 
+7
source

you can use something like this:

 var rx = new Regex("search.*text", RegexOptions.IgnoreCase); List<string> lists=new List<string>(){"search text","search this text","search my book"}; var result = lists.Where(x => rx.IsMatch(x)); 

If you get input in the form of "search% text", you can simply replace the string "%" with ". *" And use Reg ex as the template.

0
source

Source: https://habr.com/ru/post/985305/


All Articles