如何动态创建谓词
您好,我想使用谓词表达式基于搜索字符串创建一个列表。
我有一个包含不同名称的类型产品列表。
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)结果,但我想要所有匹配字符串的列表
请回答这个问题因为我正在尝试这个但我无法得到结果。
请提供一些帮助谢谢。
Hi i want to create a list based on the search string using predicate expressions.
I have a list of type products contains different names.
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;
Now i want to filter the content based on user input.User will enter n no of strings with '+' as separator. After receiving the strings i will pass them to predicate object like this
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;
}
}
If i enter more than one string(sowmya+jane+john) its giving only the last string(john) result but i want a list of all matching strings
Please answer this question because i'm trying this but i couldn't get the result.
Please do some help thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将谓词初始化为 false
您需要使用
Or
谓词生成器源 这里。它是 LINQKit
代码的一部分,以防链接失效
Initialize the predicate as false
You need to combine the predicates using
Or
Source for predicate builder here. It is part of LINQKit
Code, in case link goes
您不必在这里构建谓词。您可以尝试像这样的
list2 将包含“kumar”和“ramya”。
You don't have to build a predicate here. You can try something like this
list2 will contain "kumar" and "ramya".
由于我不确定谓词实例是否有 And 方法,我建议您使用以下代码:
As I am not sure that predicate instance has an And method, I suggest you use this code: