c#中的列表搜索

本文关键字:搜索 列表 | 更新日期: 2023-09-27 17:49:57

我有一个列表在我的c# web应用程序。这个列表由从数据库中获取的关键字填充。现在我想在列表中搜索,因为我有多个关键字搜索。

如果person类型为蓝色,那么所有匹配蓝色的记录都将被提取到列表中。现在,当他输入第二个关键字时,假设黄色,那么列表应该被过滤,它必须只包含那些具有蓝色和黄色的记录,第三和第四个关键字也是如此。

下面是代码接下来我要做什么?

public override List<ThumbNail> GetSearchResults(string search1, string path)
        {
  List<ThumbNail> mylist = new List<ThumbNail>();
       if (search1.Length - 1 == search1.LastIndexOf(","))
       search1 = search.Remove(search1.Length - 1);     
  List<string> search = new List<string>(search1.Split(','));
    for(int i=0; i <search1.Count;i++)
{
      OleDbCommand sqlcmdCommand1 = 
      new OleDbCommand("select Name,File,keyword from table1 where keyword like @searchone", mycon);
 sqlcmdCommand1.Parameters.AddWithValue("searchone", search[i]);
 sqlcmdCommand1.CommandType = CommandType.Text;
 OleDbDataReader sdaResult = sqlcmdCommand1.ExecuteReader();
 while (sdaResult.Read())
 {
  mytable thmb = new Thumbl();
  thmb.Name = sdaResult.GetString(0);
  thmbN.File = sdaResult.GetString(1);
  thmb.keyword = sdaResult.IsDBNull(3) ? "" : sdaResult.GetString(6);
  mylist.Add(thmb);
 }

}

sdaResult.Close ();
return mylist;
}
public class Thumbnail
{
        public int Id { get; set; }
        public string Name { get; set; }
        public string File { get; set; }
        public string keyword { get; set; }
        public string path { get; set; }
   }

当用户输入第一个关键字时执行此操作。所有匹配第一个关键字的记录现在都在mylist中,现在当用户键入第二个关键字时,必须在匹配第一个和第二个关键字的列表上执行搜索,并返回结果。

The DB table look like this:
id   name   keyword  File  Fkey
1     a      yellow  c:/   20
2     a      blue    c:/   20
3     a      Pinky   c:/   20
4     b      orange  c:/   21
5     b      Redish  c:/   21

c#中的列表搜索

EDIT:在您更新您的问题后,我的答案不再适用,因为您的表结构与我预期的不同。如果您的表看起来像这样,我的答案将起作用:

id   name   keywords            File 
1     a     yellow blue pinky   c:/  
2     b     orange reddish      c:/  

旧答案:首先,您还需要选择关键字,以便您可以进行进一步的过滤。

thmb.Keywords = sdaResult.GetString(someIndex);

然后,您可以使用LINQ创建列表的过滤版本…

var filtered = mylist.Where(thmb => thmb.Keywords.Contains(search2));

…一旦输入新的搜索关键字,您可以进一步过滤。

var filtered = filtered.Where(thmb => thmb.Keywords.Contains(search3));

filteredIEnumerable。如果你需要一个列表,请使用.ToList()


NEW ANSWER:基本上,您需要对表结构做的是:

  1. 从数据库中进行第二次读取,生成包含第二个关键字和

  2. 的名称列表
  3. 删除第一个列表中不包含在第二个列表中的所有列表项。例如,如果包含第二个关键字的名称列表是名为narrowingSearchList<string>,则可以执行

    var filteredList = mylist.Where(thmb => narrowingSearch.Contains(thmb.Name));
    

EDIT:现在您有了一个通用的FK,您应该能够通过使用更高级的SQL来做到这一点,例如:

SELECT name, file
  FROM table1
 WHERE keyword like @search0
   AND fk IN (SELECT fk FROM table1 WHERE keyword LIKE @search1)
   AND fk IN (SELECT fk FROM table1 WHERE keyword LIKE @search2)
   ...

你可以像这样在循环中构造这个SQL:

string[] search = search1.Split(',');
OleDbCommand cmd = new OleDbCommand("SELECT name, file FROM table1 WHERE keyword LIKE @search0", mycon);
cmd.Parameters.AddWithValue("@search0", search[0]);
for (int i = 1; i < search.Length; i++) {
    cmd.CommandText += " AND fk IN (SELECT fk FROM table1 WHERE keyword LIKE @search" + i.ToString() + ")";
    cmd.Parameters.AddWithValue("@search" + i.ToString(), search[i]);
}