简体   繁体   English

查找重复单词的值

[英]Find values where a word is repeated

Is there a way in LINQ to Entities (SQL) to identify all records where a specified word is repeated at least/less than a specified number of times? LINQ to Entities (SQL) 中是否有办法识别指定单词重复至少/少于指定次数的所有记录?

I can do this in memory, looking for at least 3 instances of the word "word" using:我可以在 memory 中执行此操作,使用以下方法查找单词“word”的至少 3 个实例:

Where(Function(x) x.Description.Split("word").Count > 3)

However the Split() function cannot translate to a SQL equivalent, and so this can only be executed in-memory, which is excruciatingly slow by the time any number of records are involved.但是,Split() function 不能转换为 SQL 等效项,因此只能在内存中执行,这在涉及任意数量的记录时速度非常慢。

The SQL to do this would be something like执行此操作的 SQL 类似于

WHERE Description LIKE '%word%word%word%'

And that's as far as I get - I can't work out how to get that SQL generated by LINQ to Entities.据我所知 - 我无法弄清楚如何将 LINQ 生成的 SQL 发送给实体。 I tried the ugly hacky .Where(Function(x) x.Description.Contains("word%word%word") on the off chance, but I'm almost relieved that it doesn't work!我偶然尝试了丑陋的.Where(Function(x) x.Description.Contains("word%word%word") ,但我几乎松了一口气,因为它不起作用!

Well it seems there is no way to express the required LIKE clause in LINQ to Entity... But what about using some other logic to detect "3 words" in the string.. something like:好吧,似乎没有办法将 LINQ 中所需的 LIKE 子句表达为实体......但是使用其他一些逻辑来检测字符串中的“3个单词”怎么样......就像:

Where(s => s.Description.Replace(" ", "").Replace("word", "").Length == s.Description.Replace(" ", "").Length - ("word".Length * 3) )

This is completely turned into SQL.这完全变成了SQL。

You can use Regular expression for that:您可以为此使用正则表达式:

.Where(Regex.Match(".+word.+word.+word.+",x.Description)

I'm not quite sure about syntax, but i think you get the idea.我不太确定语法,但我想你明白了。

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

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