我有一个大问题.
我有一个linq查询,它只是看起来像这样:
from xx in table
where xx.uid.ToString().Contains(string[])
select xx
Run Code Online (Sandbox Code Playgroud)
string[]
数组的值将是(1,45,20,10等...)
为默认.Contains
的.Contains(string)
.
我需要它来代替: .Contains(string[])
...
编辑:一位用户建议为其编写扩展类string[]
.我想知道如何,但任何人都愿意指出我正确的方向?
编辑: uid也是一个数字.这就是它被转换为字符串的原因.
帮助任何人?
无法使其工作:
/// <summary>
/// Retrieve search suggestions from previous searches
/// </summary>
public static string[] getSearchSuggestions(int SectionID, string Query)
{
string[] Suggestions;
string[] Words = Query.Split(' ');
using (MainContext db = new MainContext())
{
Suggestions = (from c in db.tblSearches
where c.SectionID == SectionID &&
Words.Any(w => c.Term.Contains(w))
select c.Term).ToArray();
}
return Suggestions;
}
Run Code Online (Sandbox Code Playgroud)
我明白了:
System.NotSupportedException:除Contains运算符外,本地序列不能用于查询运算符的LINQ to SQL实现.
我想返回字段c.Term
包含Words
数组中任何单词的记录.我也希望按照比赛总数排序,但这看起来真的很难!我找到了这个MSDN.但我也无法使用我的查询.也找到了这个,但它不起作用.
假设我有一个表dataContext.Customer,其中包含以下字段
FName varchar
LName varchar
Phone varchar
DOB datetime
Address varchar
Run Code Online (Sandbox Code Playgroud)
该表填充了一些示例数据,让我们说:
John | Smith | 3051112222 | 01/01/1978 | Roosevelt Av 787
Aron | Frank | 7871112222 | 01/01/1979 | Lambda Street 305
Dick | Bush | 9512221111 | 01/01/1980 | John Street 1
John | Allen | 7872222222 | 01/01/1981 | Liberty Av 555
Run Code Online (Sandbox Code Playgroud)
我们还有一个包含任意数量元素的字符串数组,例如:
search[0] = "1978"
search[1] = "John"
Run Code Online (Sandbox Code Playgroud)
我需要一个LINQ查询,它将使用"contains"或"any"(在SQL中表示LIKE)逐步比较表的每个字段与字符串数组中的每个项目,并仅返回与记录中所有给定条件匹配的行,基于之前的search []示例,LINQ查询应仅返回记录#1.
另一个例子可以是:
search[0] = "Bush"
search[1] = "111"
search[2] = "John"
Run Code Online (Sandbox Code Playgroud)
并且只返回记录#3.最后:
search[0] = "John"
Run Code Online (Sandbox Code Playgroud)
记录#1,#3和#4应该返回(我认为这个想法很清楚) …
我设置了一个搜索文本框,其中搜索将单独抓取每个单词并使用“包含”搜索字段。
有没有办法通过 Contains 搜索字符串数组?
//Keep in mind that the array would be generated dynamically through textbox
string[] searchWords = { "hello", "world", "today" };
var articles = _swmDbContext.Articles
.Include(c => c.Category)
.Where(a => a.Title.Contains(searchWords));
Run Code Online (Sandbox Code Playgroud)
searchWords 显然不起作用,但试图展示我想要实现的目标。searchWords[0] 之所以有效,是因为它只是一个单词。
我还按照其他链接中的建议尝试了下面的操作,但现在当我运行调试器或探查器时,WHERE 子句不会显示在查询中:
`var articles = _swmDbContext.Articles
.Include(c => c.Category)
.Where(a => searchWords.Any(w => a.Title.Contains(w)));
Run Code Online (Sandbox Code Playgroud)
`