简体   繁体   English

Linq Query Where()SQL%等效

[英]Linq Query Where() SQL % equivalent

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'. 我们可以在LINQ表达式中使用方法.Contains(string) ,听起来像'%search text%',方法.StartsWith(string) ,听起来像'search text%',方法.EndsWith(string) ,听起来像'%search text'。

But I need something that sounds '%search%text%', that finds all content containing 'search' and 'text', but not sequential. 但是我需要一些听起来像'%search%text%'的内容,它会找到包含'search'和'text'的所有内容,但不是顺序的。

Example: I have these records: 示例:我有这些记录:

search my text 搜索我的文字

search for the text 搜索文本

seek the text 寻求文本

In SQL, the query with LIKE '%search%text%' brings: 在SQL中, LIKE '%search%text%'的查询带来:

search my text 搜索我的文字

search for the text 搜索 文本

But not brings the 'seek the text' . 但不会带来'seek the text'

Any ideas? 有任何想法吗?

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;

you could 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 getting input in the form of "search%text", then you could just string replace "%" with ".*" and use as Reg ex pattern. 如果您以“search%text”的形式获得输入,那么您可以将“%”字符串替换为“。*”并使用Reg ex pattern。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM