在嵌套对象上的IQueryable上动态创建lambda搜索

时间:2015-07-27 05:16:23

标签: c# entity-framework reflection lambda

我正在尝试在嵌套对象上构建动态搜索,稍后将其发送到EF和SQL Server。到目前为止,我能够搜索第一个对象的所有属性。这是一个非常简化的版本:

public class User
{
    public string Name { get; set; }
    public Address Address { get; set; }
}
public class Address
{
    public string City { get; set; }
}

public class MyClass<TEntity> where TEntity : class {
    public IQueryable<TEntity> applySearch(IQueryable<TEntity> originalList, string propName, string valueToSearch) {

        ParameterExpression parameterExpression = Expression.Parameter(typeof(TEntity), "p");
        PropertyInfo propertyInfo = typeof(TEntity).GetProperty(propName);
        MemberExpression member = Expression.MakeMemberAccess(parameterExpression, propertyInfo);
        lambda = Expression.Lambda<Func<TEntity, bool>>(Expression.Equal(member, Expression.Constant(valueToSearch)), parameterExpression);

        return originalList.Where(expression);
    }
}

propName = "Name"一切正常时,propName = "Address.City"时,propertyInfo为空,我在member分配行上收到此错误:

  

System.ArgumentNullException:值不能为null

我能够使用此answer中的解决方案获取嵌套属性的propertyInfo

PropertyInfo propertyInfo = GetPropertyRecursive(typeof(TEntity), propName);
...

private PropertyInfo GetPropertyRecursive(Type baseType, string propertyName)
{
    string[] parts = propertyName.Split('.');

    return (parts.Length > 1)
        ? GetPropertyRecursive(baseType.GetProperty(parts[0]).PropertyType, parts.Skip(1).Aggregate((a, i) => a + "." + i))
        : baseType.GetProperty(propertyName);
}

但是我在member作业时遇到了这个错误:

  

System.ArgumentException:没有为'User'类型定义属性'System.String City'

这应该指向Address而不是User,但我不知道我是否在正确的轨道上,我的意思是,我现在应该更改parameterExpression吗?

如何对嵌套对象进行动态搜索,以便将其转换为lambda表达式,然后再发送给SQL?

3 个答案:

答案 0 :(得分:4)

在Kobi的建议和大量的反复试验之后,我终于得到了这个工作。这使用Universal PredicateBuilder。这是:

public class MyClass<TEntity> where TEntity : class
{
    public IQueryable<TEntity> ApplySearch(IQueryable<TEntity> originalList, string valueToSearch, string[] columnsToSearch)
    {

        Expression<Func<TEntity, bool>> expression = null;

        foreach (var propName in columnsToSearch)
        {
            Expression<Func<TEntity, bool>> lambda = null;

            ParameterExpression parameterExpression = Expression.Parameter(typeof(TEntity), "p");

            string[] nestedProperties = propName.Split('.');
            Expression member = parameterExpression;
            foreach (string prop in nestedProperties)
            {
                member = Expression.PropertyOrField(member, prop);
            }

            var canConvert = CanConvertToType(valueToSearch, member.Type.FullName);

            if (canConvert)
            {
                var value = ConvertToType(valueToSearch, member.Type.FullName);
                if (member.Type.Name == "String")
                {
                    ConstantExpression constant = Expression.Constant(value);
                    MethodInfo mi = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) });
                    Expression call = Expression.Call(member, mi, constant);

                    lambda = Expression.Lambda<Func<TEntity, bool>>(call, parameterExpression);
                }
                else
                {
                    lambda = Expression.Lambda<Func<TEntity, bool>>(Expression.Equal(member, Expression.Constant(value)), parameterExpression);
                }
            }

            if (lambda != null)
            {
                if (expression == null)
                {
                    expression = lambda;
                }
                else
                {
                    expression = expression.Or(lambda);
                }
            }
        }

        if (expression != null)
        {
            return originalList.Where(expression);
        }

        return originalList;
    }
}

private bool CanConvertToType(object value, string type)
{
    bool canConvert;
    try
    {
        var cValue = ConvertToType(value, type);
        canConvert = true;
    }
    catch
    {
        canConvert = false;
    }
    return canConvert;
}

private dynamic ConvertToType(object value, string type)
{
    return Convert.ChangeType(value, Type.GetType(type));
}

答案 1 :(得分:1)

提前警告 - 我没有构建表达式,只是检查它的结构。

当我需要动态创建表达式时,我发现检查表达式并复制其结构很有用:

Expression<Func<User, string>> getCity = user => user.Address.City;

现在你可以简单地调试它,例如在即时窗口中( ctrl alt i 这里):

getCity
{user => user.Address.City}
    Body: {user.Address.City}
    CanReduce: false
    DebugView: ".Lambda #Lambda1<System.Func`2[ConsoleApplication1.User,System.String]>(ConsoleApplication1.User $user) {\r\n    ($user.Address).City\r\n}"
    Name: null
    NodeType: Lambda
    Parameters: Count = 1
    ReturnType: {Name = "String" FullName = "System.String"}
    TailCall: false

我们可以看到getCity是一个带有一个参数的Lambda。让我们检查它的身体:

getCity.Body
{user.Address.City}
    CanReduce: false
    DebugView: "($user.Address).City"
    Expression: {user.Address}
    Member: {System.String City}
    NodeType: MemberAccess
    Type: {Name = "String" FullName = "System.String"}

getCity.Body是会员访问权限 - 它会访问表达式City的成员user.Address。从技术上讲,这是一个PropertyExpression,这是一个内部类,所以我们甚至无法投入它,但那没关系。
最后,让我们看一下内在表达:

((MemberExpression)getCity.Body).Expression
{user.Address}
    CanReduce: false
    DebugView: "$user.Address"
    Expression: {user}
    Member: {ConsoleApplication1.Address Address}
    NodeType: MemberAccess
    Type: {Name = "Address" FullName = "ConsoleApplication1.Address"}

那只是user.Address

现在我们可以建立一个相同的表达式:

var addressProperty = typeof (User).GetProperty("Address");
var cityProperty = typeof(Address).GetProperty("City");
var userParameter = Expression.Parameter(typeof (User), "user");
var getCityFromUserParameter = Expression.Property(Expression.Property(userParameter, addressProperty), cityProperty);
var lambdaGetCity = Expression.Lambda<Func<User, string>>(getCityFromUserParameter, userParameter);

Expression.MakeMemberAccess也适用,而不是Expression.Property

显然,你需要在循环中构建表达式,并且更加动态,但结构是相同的。

答案 2 :(得分:0)

值得一看Linqkit的谓词构建器......

http://www.albahari.com/nutshell/predicatebuilder.aspx

我还要看一下Entity SQL ...

https://msdn.microsoft.com/en-us/library/vstudio/bb387145(v=vs.100).aspx

您可能正在使用您正在编写的代码重新发明轮子。

另外,我应该根据SQL Server计划缓存发表评论,除非您没有其他选择,否则我不会动态构建查询。您最好创建一个单独的查询来处理SQL Server可以缓存计划的所有情况,如果每次执行时查询运行速度都会慢很多,那么SQL Server中的计划就不会受到影响&#39 ; s计划缓存。