2016-12-25 154 views
0

我有一個數據視圖,它由使用字符串列表的對象填充。我想通過在文本框中輸入搜索詞來選擇合適的條目。如何過濾使用字符串列表的字符串列表的列表

我的對象:

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... 
} 

希望你能幫助我。

回答

0

爲什麼你不能使用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; 
     }   
    } 
} 
+0

感謝您的快速回答。 –

+0

不幸的是,這種解決方案只適用於整個單詞... –

0

您可以聯盟在一個列表中的所有子表的,然後應用任何擴展的單詞列表搜索

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); 
} 

但是我會針對更傳統的解決方案測試性能

+0

感謝您的快速回答。 它的工作速度非常快,但不幸的是它只適用於整個單詞,並選擇單詞x或單詞y爲真的項目。我想只有單詞x和單詞y是真的... –

+0

那麼,對於第一個問題,我們可以用IndexOf替換Contains,第二個問題不是很清楚。你能解釋一下你對x字和y字有什麼意義嗎? – Steve

+0

好的,indexof是清楚的,並與字x和y我的意思是搜索字 –

0

這樣做的一種方法是重構您的類以允許搜索。這裏有一個使用索引的例子:

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; 
    } 
} 
相關問題