C# 如何使用字符串列表筛选字符串列表列表

C# 如何使用字符串列表筛选字符串列表列表,c#,string,list,search,filter,C#,String,List,Search,Filter,我有一个数据视图,它由一个使用字符串列表的对象填充。我想通过在文本框中键入搜索词来选择合适的条目 我的目标: class my_object { List<string> column1 = new List<string>(); List<string> column2 = new List<string>(); List<string> column3 = new List<string>();

我有一个数据视图,它由一个使用字符串列表的对象填充。我想通过在文本框中键入搜索词来选择合适的条目

我的目标:

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>(); 
}
class my\u对象
{
列表列1=新列表();
列表列2=新列表();
列表列3=新列表();
列表列4=新列表();
}
数据视图的我的条目:

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...
}
内部静态列表搜索对象(此列表源,列表搜索词)
{
列表结果=新列表();
foreach(源中的my_对象m)
{
foreach(SearchWords中的字符串s)
{
//过滤柱1
foreach(m.column1中的字符串c1)
{
如果(c1.IndexOf(s)!=-1)
{
结果:增加(m);
打破
}
}
} 
}
返回结果;
//问题:
//此函数仅过滤第一列。
//如果我想过滤下一列,我必须打破所有的'foreach'块
//除了“源中的my_对象m”块。。。
//如果“中断”对多个循环有效,则此方法将有效。。。
}

希望您能帮助我。

为什么您不能像这样使用
Contains()
方法

foreach (my_object m in Source)
{
    foreach(string s in SearchWords)
    {
        If(m.column1.Contains(s) || m.column2.Contains(s) || m.column3.Contains(s))
        {
             results.Add(m);   
             break;
        }         
    } 
}

您可以将所有子列表合并到一个列表中,然后对要搜索的单词列表应用Any扩展名

foreach (my_object m in Source)
{
    List<string> allStrings = m.Column1.Union(m.Column2)
                                       .Union(m.Column3)
                                       .Union(m.Column4)
                                       .ToList();
    bool exists = SearchWords.All(x => allStrings.IndexOf(x) != -1));    
    if(exists)
        results.Add(m); 
}
foreach(源代码中的my_对象m)
{
列出所有字符串=m.Column1.Union(m.Column2)
.工会(m.3)
.工会(m.4)
.ToList();
bool exists=SearchWords.All(x=>allStrings.IndexOf(x)!=-1));
如果(存在)
结果:增加(m);
}

不过,我会针对更传统的解决方案测试性能,一种方法是重构类以允许搜索。下面是一个使用索引的示例:

class my_object
{
    class Coordinates
    {
        public int _column = 0;
        public int _row = 0;
        public Coordinates(int row, int column)
        {
            _column = column;
            _row = row;
        }
    }
    List<List<string>> columns = new List<List<string>>(4);
    Dictionary<string, List<Coordinates>> searchIndex = new Dictionary<string, List<Coordinates>>();
    public my_object()
    {
        for (int i = 0; i < columns.Count; i++)
        {
            columns[i] = new List<string>();
        }
    }
    //This will create entries for each consecutive substring in each word that you add.
    public void AddWord(string word, int column)
    {
        for (int i = 0; i < word.Length; i++)
        {
            int limit = word.Length - i;
            for (int j = 1; j < limit; j++)
            {
                string temp = word.Substring(i, j);
                if (!searchIndex.ContainsKey(temp))
                {
                    searchIndex.Add(temp, new List<Coordinates>());
                }
                searchIndex[temp].Add(new Coordinates(columns.Count, column));
            }

        }
        columns[column].Add(word);
    }
    //This will return a list of list of strings that contain the search term.
    public List<List<string>> Find(string term)
    {
        List<List<string>> outVal = new List<List<string>>(4);
        if(searchIndex.ContainsKey(term))
        {
            foreach(Coordinates c in searchIndex[term])
            {
                outVal[c._column].Add(columns[c._column][c._row]);
            }
        }
        return outVal;
    }
}
class my\u对象
{
类坐标
{
公共int_列=0;
公共int_行=0;
公共坐标(int行,int列)
{
_列=列;
_行=行;
}
}
列表列=新列表(4);
字典搜索索引=新字典();
公共my_对象()
{
for(int i=0;i
感谢您的快速回答。它的工作速度非常快,但不幸的是,它只适用于整个单词,并选择单词x或单词y为真的项目。我只想得到单词x和单词y为真的项……好吧,对于第一个问题,我们可以用IndexOf替换Contains,第二个问题不是很清楚。你能解释一下x和y是什么意思吗?好的,indexof很清楚,对于x和y我指的是搜索词库,但我还是不明白。SearchWords是一个字符串列表。如果您发现my_对象m的任何子列表中包含此列表中的单词,您希望将当前m添加到结果中,然后传递到下一个my_对象实例。此时,如果my_对象的同一实例中包含另一个SearchWords单词,则搜索无效,因为您已将当前my_对象m添加到结果列表中。不,我还没有真正理解你的问题。如果搜索词中的每个词都包含了,我只想将my_对象添加到结果中