玉家伙这是没有意义的......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();
}
现在的总体思路是,从公司的名单我想所有包含所有我在ui的文本框中提供的搜索字词中的关键字。
我的问题是这样的“包含” ......如果我在名称字符串说“公司”和我搜索“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();
谢谢你大家:)
你应该把这个查询放在循环内部:results = results.where(comp.Name.Contains(searchTerms [i])),并删除“Tolist”。循环后只调用一次.tolist一次。也许它不会解决你的问题,但我认为它是正确的做法。 – Jonathan 2011-03-11 15:42:15
hi jonathon ... ToList调用确保来自每个循环操作的过滤结果始终是类型列表而不是ResultGroup,或者无论linq本身返回什么(它看起来像一个数组列表或字典对象)。此调用可确保在此代码之后查询并最终在我的数据绑定过程中使用相同类型。 –
War
2011-03-11 17:00:34