0

私はここでMarcGravellによってStackoverflowで与えられたコードを使用します:http: //goo.gl/57nW2

コード :

var param = Expression.Parameter(typeof (Foo));
var pred = Expression.Lambda<Func<Foo, bool>>(
    Expression.Call(
        Expression.PropertyOrField(param, fieldName),
        "StartsWith",null,
        Expression.Constant(stringToSearch)), param);

今、私はいくつかの議論を組み合わせたいと思います、サンプル:

    public void BuildPredicate(string[] typeSearch, string[] field, string searchValue)
    {
       //Content
       //typeSearch = new string[] {"Contains", "StartsWith", "StartsWith" };
       //field = new string[] { "FieldA", "FieldB", "FieldC" };

      //FieldA contains searchValue and FieldB startWith searchValue and FieldC startWith searchValue
    }

アイデア ?

ありがとう、

4

1 に答える 1

2

すべてのフィールドのすべての操作をループして、タイプとフィールドの組み合わせごとにOrElse句を含む式ツリーを構築するだけです。

var expressions = from type in typeSearch
                  from field in fields
                  select Expression.Call(
                    Expression.PropertyOrField(param, field),
                    type, null,
                    Expression.Constant(stringToSearch));

Expression body = Expression.Constant(false);
foreach (Expression expression in expressions)
{
    body = Expression.OrElse(body, expression);
}

var result = Expression.Lambda<Func<Foo, bool>>(body, param);

そして、要求に応じて、ToUpperへの呼び出しを含む例:

var expressions = from type in typeSearch
                  from field in fields
                  select Expression.Call(
                    Expression.Call(
                        Expression.PropertyOrField(param, field),
                        "ToUpper", null),
                    type, null,
                    Expression.Constant(stringToSearch));
于 2012-07-05T05:44:42.693 に答える