C#如何创建更复杂的.Any()表达式

时间:2019-01-23 10:33:48

标签: c# linq generics expression any

我有这个具有属性Name,value和propertyType的过滤器数组。 基于PropertyType,我构建了一个Expression,最后有了一个表达式列表,然后与And进行聚合。

return filter?
.Where(f => !string.IsNullOrEmpty(f.Value))?
.Select(f =>
{
  var parameter = Expression.Parameter(typeof(T), "item");
  var property = Expression.Property(parameter, PascalCase(f.PropertyName));

  Expression expression = null;

  if (f.Type == PropertyTypeEnum.String)
  {
      var toLowerMethod = typeof(string).GetMethod("ToLower", new Type[] { });
      var startsWithMethod = typeof(string).GetMethod("StartsWith", new[] { typeof(string) });
      var value = Expression.Constant(f.Value.ToLower());
      var loweredProperty = Expression.Call(property, toLowerMethod);

      expression = Expression.Call(loweredProperty, startsWithMethod, value);
   }
   else
   {
     var value = Convert.ChangeType(f.Value, property.Type);

     ConstantExpression constantExpression = Expression.Constant(value);
     expression = Expression.Equal(property, constantExpression);
   }

   return Expression.Lambda<Func<T, bool>>(expression, parameter);
 });

我将它们放在Linq中。如果枚举类型是字符串,它看起来像这样

.Where(x => x.ToLower().StartsWith(someString)).

现在我可以选择PropertyType为数组,并且我想使用此表达式

(x => x.Any(y => y.ToLower().StartsWith(someString)));

但是我究竟该如何构建这样的表达式?

1 个答案:

答案 0 :(得分:0)

设法使其起作用。这是case语句中的代码

var innerProperty = Expression.PropertyOrField(parameter, PascalCase(f.PropertyName));
var innerParameter = Expression.Parameter(typeof(string), "y");

var toLowerMethod = typeof(string).GetMethod(nameof(string.ToLower), new Type[] { });
var startsWithMethod = typeof(string).GetMethod(nameof(string.StartsWith), new[] { typeof(string) });
var value = Expression.Constant(f.Value.ToLower());
var loweredProperty = Expression.Call(innerParameter, toLowerMethod);
var innerExp = Expression.Call(loweredProperty, startsWithMethod, value);
var predicate = Expression.Lambda<Func<string, bool>>(innerExp, innerParameter);

expression = Expression.Call(typeof(Enumerable), "Any", new[] { typeof(string) }, innerProperty, predicate);