【发布时间】:2017-05-10 11:33:07
【问题描述】:
我有一个使用字符串列表的对象填充的数据视图。我想通过在文本框中输入搜索词来选择合适的条目。
我的对象:
class my_object
{
List<string> column1 = new List<string>();
List<string> column2 = new List<string>();
List<string> column3 = new List<string>();
List<string> column4 = new List<string>();
}
我的数据视图条目:
List<my_object> entries = new List<my_object>();
我的目标是过滤 Windows 资源管理器中的搜索功能等条目,但不同之处在于我想包括四列,而不仅仅是带有文件名的列。 有没有可能做到这一点?
我尝试过的:
internal static List<my_object> SearchObject(this List<my_object> Source, List<string> SearchWords)
{
List<my_object> results = new List<my_object>();
foreach (my_object m in Source)
{
foreach(string s in SearchWords)
{
// Filter Column 1
foreach(string c1 in m.column1)
{
if(c1.IndexOf(s) != -1)
{
results.Add(m);
break;
}
}
}
}
return results;
// Problem:
// This function only filters the first column.
// If I want to filter the next column, I have to break all 'foreach' blocks
// except the '(my_object m in Source)' block...
// It the 'break' would work for more the one loop, this method would work...
}
希望你能帮助我。
【问题讨论】:
标签: c# string list search filter