您好我想使用谓词表达式基于搜索字符串创建一个列表。
我有一个包含不同名称的类型产品列表。
List<products> list1 = new List<products>();
list1.Add(new products("sowmya"));
list1.Add(new products("Jane"));
list1.Add(new products("John"));
list1.Add(new products("kumar"));
list1.Add(new products("ramya"));
listBox1.ItemsSource = list1;
现在我想根据用户输入过滤内容。用户将输入 n 个以“+”作为分隔符的字符串。收到字符串后,我会将它们传递给这样的谓词对象
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
List<products> list2 = new List<products>();
Expression<Func<products, bool>> predicate = PredicateBuilder.True<products>();
if (e.Key == Key.Enter)
{
string Searchstring = textBox1.Text.ToString().Trim();
string[] separator = new string[] { "+" };
string[] SearchItems=Searchstring.Split(separator,StringSplitOptions.None);
foreach (string str in SearchItems)
{
string temp = str;
predicate =p => p.Name.Contains(temp.ToLower());
}
list2 = list1.AsQueryable().Where(predicate).ToList();
listBox1.ItemsSource = list2;
}
}
如果我输入多个字符串(sowmya+jane+john),它只给出最后一个字符串(john)结果,但我想要一个所有匹配字符串的列表
请回答这个问题,因为我正在尝试这个,但我无法得到结果。
请帮忙谢谢。