c# - 构建动态表达式树以过滤集合属性

标签 c# wcf entity-framework lambda filtering

我正在尝试构建一个 lambda 表达式,它将与其他表达式组合成一个相当大的表达式树以进行过滤。在我需要按子集合属性进行过滤之前,这工作正常。

如何构建一个 Lambda 表达式,该表达式将使用 Any() 对作为根对象属性的集合属性进行过滤?

例子:

CurrentDataSource.Offices.Where(o => o.base_Trades.Any(t => t.Name == "test"))

这就是我静态构建表达式的方式,但我需要动态构建它。抱歉造成混淆。

编辑:这是我如何处理不太复杂的表达式的片段:

IQueryable<Office> officeQuery = CurrentDataSource.Offices.AsQueryable<Office>();
ParameterExpression pe = Expression.Parameter(typeof(Office), "Office");
ParameterExpression tpe = Expression.Parameter(typeof(Trades), "Trades");

Expression SimpleWhere = null;
Expression ComplexWhere = null;
foreach (ServerSideFilterObject fo in ssfo)
{
    SimpleWhere = null;
    foreach (String value in fo.FilterValues)
    {
        if (!CollectionProperties.Contains(fo.PropertyName))
        {
            //Handle singleton lambda logic here.
            Expression left = Expression.Property(pe, typeof(Office).GetProperty(fo.PropertyName));
            Expression right = Expression.Constant(value);
            if (SimpleWhere == null)
            {
                SimpleWhere = Expression.Equal(left, right);
            }
            else
            {
                Expression e1 = Expression.Equal(left, right);
                SimpleWhere = Expression.Or(SimpleWhere, e1);
            }
        }
        else
        {
            //handle inner Collection lambda logic here.
            Expression left = Expression.Property(tpe, typeof(Trades).GetProperty("Name"));
            Expression right = Expression.Constant(value);
            Expression InnerLambda = Expression.Equal(left, right);

            //Problem area.
            Expression OfficeAndProperty = Expression.Property(pe, typeof(Office).GetProperty(fo.PropertyName));
            Expression OuterLambda = Expression.Call(OfficeAndProperty, typeof(Trades).GetMethod("Any", new Type[] { typeof(Expression) } ),InnerLambda);

            if (SimpleWhere == null)
                SimpleWhere = OuterLambda;
            else
                SimpleWhere = Expression.Or(SimpleWhere, OuterLambda);
        }
    }
    if (ComplexWhere == null)
        ComplexWhere = SimpleWhere;
    else
        ComplexWhere = Expression.And(ComplexWhere, SimpleWhere);
}
MethodCallExpression whereCallExpression = Expression.Call(typeof(Queryable), "Where", new Type[] { officeQuery.ElementType }, officeQuery.Expression, Expression.Lambda<Func<Office, bool>>(ComplexWhere, new ParameterExpression[] { pe }));
results = officeQuery.Provider.CreateQuery<Office>(whereCallExpression);

最佳答案

找到解决方案。我之前没有在正确的地方寻找 any 方法。

Expression left = Expression.Property(tpe, typeof(Trades).GetProperty("Name"));
Expression right = Expression.Constant(value);
Expression InnerLambda = Expression.Equal(left, right);
Expression<Func<Trades, bool>> innerFunction = Expression.Lambda<Func<Trades, bool>>(InnerLambda, tpe);

method = typeof(Enumerable).GetMethods().Where(m => m.Name == "Any" && m.GetParameters().Length == 2).Single().MakeGenericMethod(typeof(Trades));
OuterLambda = Expression.Call(method, Expression.Property(pe, typeof(Office).GetProperty(fo.PropertyName)),innerFunction);

关于c# - 构建动态表达式树以过滤集合属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8977895/

相关文章:

c# - WCF 数据服务 : SaveChanges: An error occurred while processing this request

c# - Entity Framework 代码优先 ASP.Net 和 DbContext

c# - DataGrid 显示图像的路径而不是图像本身

c# - 需要帮助构建性能最佳的 SQL 查询

jquery - 格式化 Jquery AJAX post 以将数据发送到 WCF 服务

.NET 紧凑框架中的 WCF 托管

c# - 从后面的代码显示一个 div 标签

c# - 为什么用户控件不从 View 模型绑定(bind)?

asp.net-mvc - 与 Entity Framework 一起使用的没有存储库模式的依赖注入(inject)

asp.net - Entity Framework Core 中的两个外键