string LINQ:实体字符串字段包含任何字符串数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1757214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LINQ: Entity string field contains any of an array of strings
提问by Steve Macdonald
I want to get a collection of Product entities where the product.Description property contains any of the words in a string array.
我想获取 Product 实体的集合,其中 product.Description 属性包含字符串数组中的任何单词。
It would look something like this (result would be any product which had the word "mustard OR "pickles" OR "relish" in the Description text):
它看起来像这样(结果将是描述文本中包含“芥末或“泡菜”或“津津有味”一词的任何产品):
Dim products As List(Of ProductEntity) = New ProductRepository().AllProducts
Dim search As String() = {"mustard", "pickles", "relish"}
Dim result = From p In products _
Where p.Description.Contains(search) _
Select p
Return result.ToList
I already looked at this similar questionbut couldn't get it to work.
我已经看过这个类似的问题,但无法让它发挥作用。
回答by Grizzly
Since you want to see if search contains a word which is contained in the description of p you basically need to test for each value in search if it is contained in the description of p
由于您想查看搜索是否包含 p 的描述中包含的单词,因此您基本上需要测试搜索中的每个值是否包含在 p 的描述中
result = from p in products
where search.Any(val => p.Description.Contains(val))
select p;
This is c# syntax for the lambda method since my vb is not that great
这是 lambda 方法的 c# 语法,因为我的 vb 不是那么好
回答by jason
Dim result = From p in products _
Where search.Any(Function(s) p.Description.Contains(s))
Select p
回答by Nilesh Moradiya
You can use a simple LINQ query, if all you need is to check for substrings:
如果您只需要检查子字符串,您可以使用简单的 LINQ 查询:
var q = words.Any(w => myText.Contains(w));
// returns true if myText == "This password1 is weak";
If you want to check for whole words, you can use a regular expression:
如果要检查整个单词,可以使用正则表达式:
Matching against a regular expression that is the disjunction of all the words:
// you may need to call ToArray if you're not on .NET 4 var escapedWords = words.Select(w => @"\b" + Regex.Escape(w) + @"\b"); // the following line builds a regex similar to: (word1)|(word2)|(word3) var pattern = new Regex("(" + string.Join(")|(", escapedWords) + ")"); var q = pattern.IsMatch(myText);
Splitting the string into words with a regular expression, and testing for membership on the words collection (this will get faster if you use make words into a
HashSet
instead of aList
):var pattern = new Regex(@"\W"); var q = pattern.Split(myText).Any(w => words.Contains(w));
匹配所有单词的分离的正则表达式:
// you may need to call ToArray if you're not on .NET 4 var escapedWords = words.Select(w => @"\b" + Regex.Escape(w) + @"\b"); // the following line builds a regex similar to: (word1)|(word2)|(word3) var pattern = new Regex("(" + string.Join(")|(", escapedWords) + ")"); var q = pattern.IsMatch(myText);
使用正则表达式将字符串拆分为单词,并测试单词集合的成员资格(如果您使用 make words into a
HashSet
而不是 a ,这会更快List
):var pattern = new Regex(@"\W"); var q = pattern.Split(myText).Any(w => words.Contains(w));
In order to filter a collection of sentences according to this criterion all you have to do its put it into a function and call Where
:
为了根据此标准过滤一组句子,您只需将其放入一个函数中并调用Where
:
// Given:
// bool HasThoseWords(string sentence) { blah }
var q = sentences.Where(HasThoseWords);
Or put it in a lambda:
或者把它放在一个 lambda 表达式中:
var q = sentences.Where(s => Regex.Split(myText, @"\W").Any(w => words.Contains(w)));
Ans From => How to check if any word in my List<string> contains in textby @R. Martinho Fernandes
Ans From =>如何检查我的 List<string> 中的任何单词是否包含在@R 的文本中。马蒂尼奥·费尔南德斯