繁体   English   中英

按nullable排序后,实体框架的导航属性为null

[英]Entity Framework's navigation properties are null after sort by nullable

我使用以下代码按表达式转换顺序,因此可空列也可以按顺序进行排序。

protected virtual Expression<Func<T, object>> GetSorting(string ordering)
{
        Expression<Func<T, object>> expression = default(Expression<Func<T, object>>);
        IEnumerable<Order> sortObjects = string.IsNullOrEmpty(ordering) ? null : JsonConvert.DeserializeObject<IEnumerable<Order>>(ordering);
        if (sortObjects != null)
        {
            foreach (Order sortObject in sortObjects)
            {
                Expression<Func<T, object>> currentExpression = this.GetExpression(sortObject.Property);
                expression = this.CombineExpressions(expression, currentExpression);
            }
        }

        return expression;
}

private Expression<Func<T, object>> GetExpression(string propertyName)
{
        Type type = typeof(T);
        ParameterExpression parameter = Expression.Parameter(type, "x");
        MemberExpression propertyReference = Expression.Property(parameter, propertyName);
        Expression conversion = Expression.Convert(propertyReference, typeof(object));
        Expression<Func<T, object>> currentExpression = Expression.Lambda<Func<T, object>>(conversion, new[] { parameter });
        return currentExpression;

}

private Expression<Func<T, object>> CombineExpressions(Expression<Func<T, object>> expression, Expression<Func<T, object>> currentExpression)
{
        if (expression == default(Expression<Func<T, object>>))
        {
            expression = currentExpression;
        }
        else
        {
            // Combine the two expressions' body together
            BinaryExpression body = Expression.AndAlso(expression.Body, currentExpression.Body);
            ParameterExpression[] parameters = new ParameterExpression[1] { Expression.Parameter(typeof(T), expression.Parameters.First().Name) };

            // Convert the BinaryExpression to the requested type
            Expression<Func<T, object>> lambda = Expression.Lambda<Func<T, object>>(body, parameters);

            expression = lambda;
        }

        return expression;
}

该代码对于所有非导航属性都非常适用,但是似乎不再需要查询导航属性了。 我使用Select表达式加载导航属性,如下所示:

protected override Expression<Func<Resource, ResourceViewModel>> Selector
{
        get
        {
            return (x) => new ResourceViewModel()
            {
                ResourceId = x.ResourceId,
                DisplayName = x.DisplayName,                  
                ResourceType = x.ResourceType != null ? x.ResourceType.Name : string.Empty,
            }
        }
}

如果我没有要订购的东西,那么将加载导航属性。 但是,一旦有什么要订购的,导航属性为null。 如果我跳过三元操作并直接转到ResourceType.Name属性,则会收到一个异常,告诉我lambda_method引发了NullReference异常。

我知道订购导航属性无法正常工作,但这不是问题。 按“常规”属性排序会导致此问题。

有什么想法吗?

事实证明,这个问题并不像我想的那么复杂。 问题是我创建了错误的表达式树。 您可以嵌套表达式,以便导航到属性的属性。

以下解决方案应对此进行解释(这不是我实际解决此问题的方式,但应明确说明):

private Expression<Func<T, object>> GetExpression(string parentClass, string propertyName)
{
    Type type = typeof(T);
    ParameterExpression parameter = Expression.Parameter(type, "x");

    // Get parent class expression
    // Will result in (x) => x.MyNavigationPropertyWhichIsAClass
    MemberExpression propertyReference1 = Expression.Property(parameter, parentclass);

    // Navigate to the property of the navigation property class
    // Will result in (x) => x.MyNavigationPropertyWhichIsAClass.MyPropertyIWantToSort
    MemberExpression propertyReference2 = Expression.Property(propertyRefernce1, propertyName);

    Expression conversion = Expression.Convert(propertyReference2, typeof(object));
    Expression<Func<T, object>> currentExpression = Expression.Lambda<Func<T, object>>(conversion, new[] { parameter });
    return currentExpression;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM