こんにちは私は述語式を使用して検索文字列に基づいてリストを作成したいと思います。
さまざまな名前が含まれているタイプの製品のリストがあります。
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)の結果のみが表示されますが、一致するすべての文字列のリストが必要です
私はこれを試していますが、結果を得ることができなかったので、この質問に答えてください。
助けてくれてありがとう。