您好我想使用谓词表达式基于搜索字符串创建列表.
我有一个类型产品列表包含不同的名称.
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;
Run Code Online (Sandbox Code Playgroud)
现在我想根据用户输入过滤内容.用户将输入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;
}
} …Run Code Online (Sandbox Code Playgroud)