如何使用与 sql 中的“%%”相同的条件搜索列表框
Posted
技术标签:
【中文标题】如何使用与 sql 中的“%%”相同的条件搜索列表框【英文标题】:How to search listbox using the same criteria as like "%%" in sql 【发布时间】:2013-02-12 21:57:11 【问题描述】:如何使用与 sql 中的 "%%" 相同的条件搜索 ListBox 例如,如果列表包含以下项目 cat , dog , cat with ring,dog with bone 并在文本框中输入“with”。我需要过滤此列表框以仅包含包含单词“with”的记录(即 cat with ring,dog with bone)。
到目前为止,我可以使用此代码搜索并选择以输入字符串开头的项目..
private void txtSearch_TextChanged(object sender, EventArgs e)
int index = lst.FindString(this.txtSearch.Text);
if (0 <= index)
lst.SelectedIndex = index;
【问题讨论】:
您的列表框是如何填充的?它是否绑定到数据源?有没有考虑用string.Contains
过滤?
***.com/questions/12068154/…
【参考方案1】:
应该这样做:
string searchTerm = this.txtSearch.Text;
var items = lst.Items.Cast<ListItem>().Where(t=>t.Value.Contains(searchTerm));
items
将包含所有具有包含您的搜索词的Value
的 ListItem。
【讨论】:
以上是关于如何使用与 sql 中的“%%”相同的条件搜索列表框的主要内容,如果未能解决你的问题,请参考以下文章