编程语言
首页 > 编程语言> > C#-ICollection列的动态Lambda表达式

C#-ICollection列的动态Lambda表达式

作者:互联网

我正在尝试生成动态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);

标签:dynamic,linq,c,lambda,expression-trees
来源: https://codeday.me/bug/20191108/2006858.html