ICollection列的动态Lambda表达式

时间:2019-01-25 10:44:49

标签: c# linq dynamic lambda expression-trees

我正在尝试生成动态lambda表达式以过滤ICollection字段。在Linq中,它看起来像这样:

.Where(x => x.Remarks.Any(s => s.Remark.Description.Contains("filter")))

我已经完成了最后一部分:s => (s.Remark.Description.Contains("filter"))

MethodInfo containsMethod = typeof(string).GetMethod("Contains", new[] { typeof(string) });
var parameterExpression = Expression.Parameter(Type.GetType("RemarksModel"), "s");
var constant = Expression.Constant("filter");
var property = Expression.Property(parameterExpression, "Remark");
property = Expression.Property(property, "Description");

var expression = Expression.Call(property, containsMethod, constant);

var lambda = Expression.Lambda<Func<remarkModel, bool>>(expression, parameterExpression);

现在我很难忍受,在其中添加带有.Any()的第一部分。

带有集合的模型如下:

public class ReadsModel { 
    public ICollection< RemarksModel > Remarks { get; set; }
}

public class RemarksModel {
    [ForeignKey("RemarkId")]
    public virtual RemarkModel Remark { get; set; }
    [ForeignKey("ReadsId")]}
    public virtual ReadsModel MeterRead { get; set; }
}

public class RemarkModel {
    public string Description { get; set; }
}

1 个答案:

答案 0 :(得分:3)

如果我们删除C#编译器扩展方法sugar并从中键入推断

x => x.Remarks.Any(lambda)

实际的表情看起来像

(ReadsModel x) => Enumerable.Any<RemarksModel>(x.Remarks, lambda)    

因此,使用Expression类进行构建的代码可能像这样

var parameter = Expression.Parameter(typeof(ReadsModel), "x");
var body = Expression.Call(
    typeof(Enumerable), // class containing the static method
    nameof(Enumerable.Any), // method name
    new Type[] { typeof(RemarksModel) }, // generic type arguments
    Expression.Property(parameter, "Remarks"), lambda // method arguments
);
var predicate = Expression.Lambda<Func<ReadsModel, bool>>(body, parameter);