LINQ to SQL选择在字符串拆分中具有任何单词的所有记录 [英] LINQ to SQL selecting all records which have any word in the string split
问题描述
string1:如何
string2:你好吗?
string1: How
string2: How Are you ?
我想要做的是通过对照string1检查string2并选择具有"How"或"Are"或"you"的任何记录来从数据库中选择所有记录.
What i want to do is select all records from the database by checking string2 against string1 and selecting any record which have either "How" or "Are" or "you".
pager1.ItemCount = appliedQuery.Where(q => q.string1.Contains(string2)).count();
请注意:String1只是一个单词,我只想获取db2中任何单词的任何出现的db中的所有记录.
PlEASE NOTE: String1 is just one word and I just want to get all records in the db with any occurance of any word in string2.
我知道string2需要拆分,但是拆分后我如何在linq查询语句中使用它来获取记录?
I understand that string2 needs to split, however after the split how do i use it in the linq query statement to fetch the records?
预先感谢
推荐答案
查看我如何构造linq查询.我希望这有帮助. 只需尝试将代码复制并粘贴到控制台应用中即可查看结果.
Check out how I constructed my linq query. I hope this helps. Just try to copy and paste the code in your console app to see the result.
class Program
{
static void Main(string[] args)
{
// List of strings that you may consider it from your
/// database which is String1
List<string> lstStrings = new List<string>();
lstStrings.Add("twenty one");
lstStrings.Add("twenty two");
lstStrings.Add("twenty three");
lstStrings.Add("twenty four");
// The string to compare to which is your String2
string strString = "one two four";
// Splitting the strings to be compared
string[] strArray = strString.Split(' ');
// The linq that helps you query the data exactly as what you wanted
var result = (from string A in lstStrings
from string B in strArray
where A.Contains(B)
select A).Distinct();
// Count result
Console.WriteLine(result.Count());
// Individual values
foreach (string str in result)
{
Console.WriteLine(str);
}
Console.ReadLine();
}
}
这篇关于LINQ to SQL选择在字符串拆分中具有任何单词的所有记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!