linq 关键字搜索包含
好吧,伙计们,这没有任何意义......
我有这个方法:
// break down the search terms in to individual keywords
string[] searchTerms = ui_txtSearch.Text.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
// get the complete list of companies
List<Company> results = Company.List();
// foreach keyword
for (int i = 0; i < searchTerms.Length; i++)
{
// results = the existing result set - the items that dont meet the current search term results.
results = (from comp in results
where comp.Name.Contains(searchTerms[i]
select comp).ToList();
}
现在的总体思路是,从公司列表中,我想要所有包含用户界面上文本框中提供的搜索词中的所有关键字的公司。
我的问题是这个“包含”(上面在 ** 中突出显示)...如果我在名称字符串中说“公司”并且我搜索“Co”,我希望结果是这样,因为名称将包含该名称,但是它没有……
有什么想法吗?
编辑:
好的,我发现问题是区分大小写,所以我将代码重构为:
// break down the search terms in to individual keywords
string[] searchTerms = ui_txtSearch.Text.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
// get the complete list of companies
List<Company> results = Company.List();
// foreach keyword
for (int i = 0; i < searchTerms.Length; i++)
{
// results = the existing result set - the items that dont meet the current search term results.
results = (
from comp in results
where comp.Name.ToLower().IndexOf(searchTerms[i].ToLower()) > -1
select comp
).ToList();
}
为了解决下面的一些反馈:
搜索词可能类似于“Test Company 1”,我正在寻找“test”的所有结果公司名称中可以找到“company”和“1”,并且结果集中必须包含完整搜索词被“ ”分割时出现的所有搜索关键字。
据我了解,最干净的方法是使用循环??? ...还是我错了?
所以我基本上将其读为...
- 获取所有公司的列表
- ,按搜索词 1 过滤列表
- ,从过滤列表中按搜索词 N 过滤...并重复,直到考虑所有术语。
- 结果集现在将包含公司名称中提供的所有搜索词。
当前的代码似乎可以工作并且可以回答我的问题......但是你们认为有更有效的方法来做到这一点吗?
感谢大家的帮助:)
编辑2:
感谢下面给出的所有帮助,我相信最终版本(仍在测试)应该是这样的:
// break down the search terms in to individual keywords
string[] searchTerms = ui_txtSearch.Text.ToLower().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
// get the complete list of companies
List<Company> results;
// results = the existing result set - the items that dont meet the current search term results.
results = (
from comp in Company.List()
where searchTerms.All(s => comp.Name.ToLower().IndexOf(s) > -1)
select comp
).ToList();
谢谢大家:)
Ok guys this makes no sense...
I have this method:
// break down the search terms in to individual keywords
string[] searchTerms = ui_txtSearch.Text.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
// get the complete list of companies
List<Company> results = Company.List();
// foreach keyword
for (int i = 0; i < searchTerms.Length; i++)
{
// results = the existing result set - the items that dont meet the current search term results.
results = (from comp in results
where comp.Name.Contains(searchTerms[i]
select comp).ToList();
}
Now the general idea is that from the list of companies I want all that contain all of the keywords in the search term provided in my textbox on the ui.
My problem is this "Contains" (highlighted above in **) ... if I have say "Company" in the Name string and I search for "Co" i expect it to that as a result because the name would contain that but it doesn't ...
any ideas ?
EDIT:
Ok i found that the problem was case sensitivity so i refactored the code to this:
// break down the search terms in to individual keywords
string[] searchTerms = ui_txtSearch.Text.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
// get the complete list of companies
List<Company> results = Company.List();
// foreach keyword
for (int i = 0; i < searchTerms.Length; i++)
{
// results = the existing result set - the items that dont meet the current search term results.
results = (
from comp in results
where comp.Name.ToLower().IndexOf(searchTerms[i].ToLower()) > -1
select comp
).ToList();
}
To address some of your feedback below:
The search term might be something like "Test Company 1", I am looking for all results where "test" and "company" and "1" can be found in the company name and the result set must contain all search keywords presented when the full search term is split by " ".
the cleanest way to do this is with a loop as i understand it??? ... or am i wrong ?
so i basically read this as ...
- get a list of all companies
- filter list by search term 1
- from filtered list filter by search term N ... and repeat until all terms are considered.
- the result set will now contain all search terms provided in the company name.
The current code seems to work and sort of answers my question ... but do you guys think theres a more efficient way to do this?
Thanks for the help all :)
EDIT 2:
Thanks to all the help given below I believe the final version (still testing) should be this:
// break down the search terms in to individual keywords
string[] searchTerms = ui_txtSearch.Text.ToLower().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
// get the complete list of companies
List<Company> results;
// results = the existing result set - the items that dont meet the current search term results.
results = (
from comp in Company.List()
where searchTerms.All(s => comp.Name.ToLower().IndexOf(s) > -1)
select comp
).ToList();
thank you everyone :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将在每次迭代中重新分配
结果
。但在我看来,您可以将整个代码替换为:这应该更符合您正在寻找的内容。
You're reassigning
results
on every iteration. But it also looks to me like you can just replace your entire code with this:That should be a little more in line with what you're looking for.
循环中的代码将最终结果分配给
results
,这也是正在搜索的数据。因此,如果Co
值是searchTerms
中的第二项,它可能找不到它,因为它会在第一次迭代时被清除。The code in the loop is assigning the final result to
results
, which is also the data being searched. So if theCo
value is the second item in thesearchTerms
, it likely won't find it since it would have been cleared on the first iteration.“结果”在每次迭代时都会重新分配,因此结果将只是数组中最后一个搜索词的结果。您还缺少“Contains”方法的右括号。在我的脑海中,我想说您可能必须执行以下操作:
使用不同的列表变量来保存结果(如“finalResults”)并仅查询原始列表。
添加到构建列表:finalResults.AddRange((linq query).ToList());
使用不同的子句过滤最终结果以剔除重复的内容
"Results" is being re-assigned on each iteration, so the results would be only the results from the last search term in the array. You're also missing a closing paren for the "Contains" method. Off the top of my head, I'd say you'd probably have to do something like:
Use a different list variable to hold the results (like "finalResults") and only query the original list.
Add to the built up list: finalResults.AddRange((linq query).ToList());
filter the final results with a distinct clause to weed out dupes