当前位置:  开发笔记 > 编程语言 > 正文

如何创建表达式树来表示C#中的'String.Contains("term")'?

如何解决《如何创建表达式树来表示C#中的'String.Contains("term")'?》经验,为你挑选了3个好方法。

我刚刚开始使用表达式树,所以我希望这是有道理的.我正在尝试创建一个表达式树来表示:

t => t.SomeProperty.Contains("stringValue");

到目前为止我有:

    private static Expression.Lambda> GetContainsExpression(string propertyName, string propertyValue)
    {
        var parameterExp = Expression.Parameter(typeof(T), "type");
        var propertyExp = Expression.Property(parameter, propertyName);
        var containsMethodExp = Expression.*SomeMemberReferenceFunction*("Contains", propertyExp) //this is where I got lost, obviously :)
        ...
        return Expression.Lambda>(containsMethodExp, parameterExp); //then something like this
    }

我只是不知道如何引用String.Contains()方法.

帮助赞赏.



1> Marc Gravell..:

就像是:

class Foo
{
    public string Bar { get; set; }
}
static void Main()
{
    var lambda = GetExpression("Bar", "abc");
    Foo foo = new Foo { Bar = "aabca" };
    bool test = lambda.Compile()(foo);
}
static Expression> GetExpression(string propertyName, string propertyValue)
{
    var parameterExp = Expression.Parameter(typeof(T), "type");
    var propertyExp = Expression.Property(parameterExp, propertyName);
    MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
    var someValue = Expression.Constant(propertyValue, typeof(string));
    var containsMethodExp = Expression.Call(propertyExp, method, someValue);

    return Expression.Lambda>(containsMethodExp, parameterExp);
}

您可能会觉得这很有帮助.


@AhmD Expression.Not围绕Expression.Call,并将GetMethod更改为StartsWith?

2> Abhijeet Pat..:

这个怎么样:

Expression> expFunc = (name, value) => name.Contains(value);

在客户端代码中:

    bool result = expFunc.Compile()("FooBar", "Foo");   //result true
    result = expFunc.Compile()("FooBar", "Boo");        //result false



3> Leng Weh Sen..:

要执行以下搜索:

ef.Entities.Where(entity => arr.Contains(entity.Name)).ToArray();

跟踪字符串将是:

SELECT .... From Entities ... Where Name In ("abc", "def", "qaz")

我使用下面创建的方法:

ef.Entities.Where(ContainsPredicate(arr, "Name")).ToArray();

public Expression> ContainsPredicate(T[] arr, string fieldname) where TEntity : class {
  ParameterExpression entity = Expression.Parameter(typeof(TEntity), "entity");
  MemberExpression member = Expression.Property(entity, fieldname);

  var containsMethods = typeof(Enumerable).GetMethods(BindingFlags.Static | BindingFlags.Public)
  .Where(m => m.Name == "Contains");
  MethodInfo method = null;
  foreach (var m in containsMethods) {
    if (m.GetParameters().Count() == 2) {
      method = m;
      break;
    }
  }
  method = method.MakeGenericMethod(member.Type);
  var exprContains = Expression.Call(method, new Expression[] { Expression.Constant(arr), member });
  return Expression.Lambda>(exprContains, entity);
}

推荐阅读
mobiledu2402851323
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有