C# LINQ to SQL选择字符串拆分中包含任何单词的所有记录
string1:如何C# LINQ to SQL选择字符串拆分中包含任何单词的所有记录,c#,string,linq,C#,String,Linq,string1:如何 string2:你好吗 我要做的是从数据库中选择所有记录,方法是将string2与string1进行对比,并选择任何具有“How”、“Are”或“you”的记录 请注意:String1只是一个单词,我只想在string2中出现任何单词时获取数据库中的所有记录 我知道string2需要拆分,但是在拆分之后,如何在linq查询语句中使用它来获取记录 提前感谢一个可能的解决方案是: foreach(string s in string2.Split(' ')) { pag
string2:你好吗 我要做的是从数据库中选择所有记录,方法是将string2与string1进行对比,并选择任何具有“How”、“Are”或“you”的记录 请注意:String1只是一个单词,我只想在string2中出现任何单词时获取数据库中的所有记录 我知道string2需要拆分,但是在拆分之后,如何在linq查询语句中使用它来获取记录
提前感谢一个可能的解决方案是:
foreach(string s in string2.Split(' '))
{
pager1.ItemCount += appliedQuery.Where(q => q.string1.Contains(s)).count();
}
一种可能的解决办法是:
foreach(string s in string2.Split(' '))
{
pager1.ItemCount += appliedQuery.Where(q => q.string1.Contains(s)).count();
}
将
string2
拆分为字符串数组:
string[] words = string2.Split(' ');
然后检查string1
是否在数组中的任何项目中:
pager1.ItemCount = appliedQuery.Where(q => words.Contains(q.string1)).Count();
将
string2
拆分为字符串数组:
string[] words = string2.Split(' ');
然后检查string1
是否在数组中的任何项目中:
pager1.ItemCount = appliedQuery.Where(q => words.Contains(q.string1)).Count();
查看我是如何构造linq查询的。我希望这有帮助。 只要试着在你的控制台应用程序中复制并粘贴代码就可以看到结果
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();
}
}
类程序
{
静态void Main(字符串[]参数)
{
/你可以从中考虑的字符串列表
///是String1的数据库
List lstStrings=新列表();
1.添加(“二十一”);
1.添加(“二十二”);
1.添加(“二十三”);
1.添加(“二十四”);
//要比较的字符串,它是您的String2
string strString=“一二四”;
//拆分要比较的字符串
字符串[]strArray=strString.Split(“”);
//linq可以帮助您精确地查询所需的数据
var result=(来自lstStrings中的字符串A
从strArray中的字符串B
其中A.包含(B)
选择A).Distinct();
//计数结果
Console.WriteLine(result.Count());
//个人价值观
foreach(结果中的字符串str)
{
控制台写入线(str);
}
Console.ReadLine();
}
}
查看我是如何构造linq查询的。我希望这有帮助。
只要试着在你的控制台应用程序中复制并粘贴代码就可以看到结果
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();
}
}
类程序
{
静态void Main(字符串[]参数)
{
/你可以从中考虑的字符串列表
///是String1的数据库
List lstStrings=新列表();
1.添加(“二十一”);
1.添加(“二十二”);
1.添加(“二十三”);
1.添加(“二十四”);
//要比较的字符串,它是您的String2
string strString=“一二四”;
//拆分要比较的字符串
字符串[]strArray=strString.Split(“”);
//linq可以帮助您精确地查询所需的数据
var result=(来自lstStrings中的字符串A
从strArray中的字符串B
其中A.包含(B)
选择A).Distinct();
//计数结果
Console.WriteLine(result.Count());
//个人价值观
foreach(结果中的字符串str)
{
控制台写入线(str);
}
Console.ReadLine();
}
}
Sostring1
在数据库中,而string2
来自另一个输入源?是的,string1在数据库中,string2来自另一个源所以string1
在数据库中,而string2
来自另一个输入源?是的,string1在数据库中,而string2来自另一个源,所以每个源都有一个查询字里行间2?我认为这不是一个好主意。那么对string2中的每个单词进行查询?我认为那不是个好主意。