我正在尝试生成动态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; }
}
最佳答案
如果我们删除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);