10

こんにちは私は述語式を使用して検索文字列に基づいてリストを作成したいと思います。

さまざまな名前が含まれているタイプの製品のリストがあります。

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)の結果のみが表示されますが、一致するすべての文字列のリストが必要です

私はこれを試していますが、結果を得ることができなかったので、この質問に答えてください。

助けてくれてありがとう。

4

3 に答える 3

23

述語をfalseとして初期化します

Expression<Func<products, bool>> predicate = PredicateBuilder.False<products>();

を使用して述語を組み合わせる必要がありますOr

foreach (string str in SearchItems)
{
    string temp = str;
    predicate = predicate.Or(p => p.NameToLower().Contains(temp.ToLower()));                   
}

述語ビルダーのソースはここにありますLINQKitの一部です

リンクが行く場合のコード

using System;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;

public static class PredicateBuilder
{
  public static Expression<Func<T, bool>> True<T> ()  { return f => true;  }
  public static Expression<Func<T, bool>> False<T> () { return f => false; }

  public static Expression<Func<T, bool>> Or<T> (this Expression<Func<T, bool>> expr1,
                                                      Expression<Func<T, bool>> expr2)
  {
    var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast<Expression> ());
    return Expression.Lambda<Func<T, bool>>
          (Expression.OrElse (expr1.Body, invokedExpr), expr1.Parameters);
  }

  public static Expression<Func<T, bool>> And<T> (this Expression<Func<T, bool>> expr1,
                                                       Expression<Func<T, bool>> expr2)
  {
    var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast<Expression> ());
    return Expression.Lambda<Func<T, bool>>
          (Expression.AndAlso (expr1.Body, invokedExpr), expr1.Parameters);
  }
}
于 2011-06-17T09:40:12.530 に答える
2

ここで述語を作成する必要はありません。あなたはこのようなことを試すことができます

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"));

string input = "aaa+kuma+ram";
List<string> searchStrings =
    input.Split(new string[] { "+" }, StringSplitOptions.None)
    .Select(s => s.ToLower())
    .ToList();

List<products> list2 = (
    from p in list1
    where searchStrings.Any(s => p.Name.Contains(s))
    select p).ToList();

list2には「kumar」と「ramya」が含まれます。

于 2011-06-17T10:16:35.940 に答える
1

述語インスタンスにAndメソッドがあるかどうかわからないため、次のコードを使用することをお勧めします。

var list = list1.AsQueryable();
foreach (string str in SearchItems)
{
     list = list.Where(p => p.Name.ToLower().Contains(str.ToLower()));
}
listBox1.ItemsSource = list.ToList();
于 2011-06-17T09:50:33.037 に答える