0

我有以下代码,我尝试重构它以删除重复的代码。但是,每次我尝试重构它时,我都会卡住。有没有人可以帮助我解决这个问题。

    public static IOrderedQueryable<T> ObjectSort<T>(this IQueryable<T> entities, Expression<Func<T, object>> expression, string order = "asc")
    {
        var sortOrder = order == "asc" ? SortOrder.Ascending : SortOrder.Descending;
        return entities.ObjectSort(expression, sortOrder);
    }

    public static IOrderedQueryable<T> ObjectThenBy<T>(this IOrderedQueryable<T> entities, Expression<Func<T, object>> expression, string order = "asc")
    {
        var sortOrder = order == "asc" ? SortOrder.Ascending : SortOrder.Descending;
        return entities.ObjectThenBy(expression, sortOrder);
    }

    public static IOrderedQueryable<T> ObjectSort<T>(this IQueryable<T> entities, Expression<Func<T, object>> expression, SortOrder order = SortOrder.Ascending)
    {
        var unaryExpression = expression.Body as UnaryExpression;
        if (unaryExpression != null)
        {
            var propertyExpression = (MemberExpression)unaryExpression.Operand;
            var parameters = expression.Parameters;

            if (propertyExpression.Type == typeof(DateTime))
            {
                var newExpression = Expression.Lambda<Func<T, DateTime>>(propertyExpression, parameters);
                return order == SortOrder.Ascending ? entities.OrderBy(newExpression) : entities.OrderByDescending(newExpression);
            }

            if (propertyExpression.Type == typeof(DateTime?))
            {
                var newExpression = Expression.Lambda<Func<T, DateTime?>>(propertyExpression, parameters);
                return order == SortOrder.Ascending ? entities.OrderBy(newExpression) : entities.OrderByDescending(newExpression);
            }

            if (propertyExpression.Type == typeof(int))
            {
                var newExpression = Expression.Lambda<Func<T, int>>(propertyExpression, parameters);
                return order == SortOrder.Ascending ? entities.OrderBy(newExpression) : entities.OrderByDescending(newExpression);
            }

            if (propertyExpression.Type == typeof(int?))
            {
                var newExpression = Expression.Lambda<Func<T, int?>>(propertyExpression, parameters);
                return order == SortOrder.Ascending ? entities.OrderBy(newExpression) : entities.OrderByDescending(newExpression);
            }

            if (propertyExpression.Type == typeof(bool))
            {
                var newExpression = Expression.Lambda<Func<T, bool>>(propertyExpression, parameters);
                return order == SortOrder.Ascending ? entities.OrderBy(newExpression) : entities.OrderByDescending(newExpression);
            }

            throw new NotSupportedException("Object type resolution not implemented for this type");
        }
        return order == SortOrder.Ascending ? entities.OrderBy(expression) : entities.OrderByDescending(expression);
    }

    public static IOrderedQueryable<T> ObjectThenBy<T>(this IOrderedQueryable<T> entities, Expression<Func<T, object>> expression, SortOrder order = SortOrder.Ascending)
    {
        var unaryExpression = expression.Body as UnaryExpression;
        if (unaryExpression != null)
        {
            var propertyExpression = (MemberExpression)unaryExpression.Operand;
            var parameters = expression.Parameters;

            if (propertyExpression.Type == typeof(DateTime))
            {
                var newExpression = Expression.Lambda<Func<T, DateTime>>(propertyExpression, parameters);
                return order == SortOrder.Ascending ? entities.ThenBy(newExpression) : entities.ThenByDescending(newExpression);
            }

            if (propertyExpression.Type == typeof(DateTime?))
            {
                var newExpression = Expression.Lambda<Func<T, DateTime?>>(propertyExpression, parameters);
                return order == SortOrder.Ascending ? entities.ThenBy(newExpression) : entities.ThenByDescending(newExpression);
            }

            if (propertyExpression.Type == typeof(int))
            {
                var newExpression = Expression.Lambda<Func<T, int>>(propertyExpression, parameters);
                return order == SortOrder.Ascending ? entities.ThenBy(newExpression) : entities.ThenByDescending(newExpression);
            }

            if (propertyExpression.Type == typeof(int?))
            {
                var newExpression = Expression.Lambda<Func<T, int?>>(propertyExpression, parameters);
                return order == SortOrder.Ascending ? entities.ThenBy(newExpression) : entities.ThenByDescending(newExpression);
            }

            if (propertyExpression.Type == typeof(bool))
            {
                var newExpression = Expression.Lambda<Func<T, bool>>(propertyExpression, parameters);
                return order == SortOrder.Ascending ? entities.ThenBy(newExpression) : entities.ThenByDescending(newExpression);
            }

            throw new NotSupportedException("Object type resolution not implemented for this type");
        }
        return order == SortOrder.Ascending ? entities.ThenBy(expression) : entities.ThenByDescending(expression);
    }

请注意,区别在于 OrderBy 和 ThenBy... 其余部分完全相同。希望有人知道重构它的方法。我试图创建一个 Func 参数来传递 OrderBy 或 ThenBy 方法。但是,我尝试的一切都被卡住了。

4

1 回答 1

1

OrderBy 和 ThenBy 的区别在于 OrderBy 期望 IQueryable,而 ThenBy 期望 IOrderedqueryAble。

现在要使用 OrderBy 或 ThenBy,您需要确定集合是 IQueryable 还是 IOrderedQueryAble。

之后,如果它是 IQueryable,则调用 OrderBy。如果是 IOrderedQueryable,则调用 ThenBy(在转换为 IOrderedQueryable 之后。

重构的代码片段变为

    public static IOrderedQueryable<T> ObjectSortRefactored<T>(this IQueryable<T> entities, Expression<Func<T, object>> expression, SortOrder order = SortOrder.Ascending)
    {
        var unaryExpression = expression.Body as UnaryExpression;
        if (unaryExpression != null)
        {
            var propertyExpression = (MemberExpression)unaryExpression.Operand;
            var parameters = expression.Parameters;

            if (propertyExpression.Type == typeof(DateTime))
            {
                var newExpression = Expression.Lambda<Func<T, DateTime>>(propertyExpression, parameters);
                return order == SortOrder.Ascending ? entities.OrderBy(newExpression) : entities.OrderByDescending(newExpression);
            }

            if (propertyExpression.Type == typeof(DateTime?))
            {
                var newExpression = Expression.Lambda<Func<T, DateTime?>>(propertyExpression, parameters);
                return order == SortOrder.Ascending ? entities.OrderBy(newExpression) : entities.OrderByDescending(newExpression);
            }

            if (propertyExpression.Type == typeof(int))
            {
                var newExpression = Expression.Lambda<Func<T, int>>(propertyExpression, parameters);
                return order == SortOrder.Ascending ? entities.OrderBy(newExpression) : entities.OrderByDescending(newExpression);
            }

            if (propertyExpression.Type == typeof(int?))
            {
                var newExpression = Expression.Lambda<Func<T, int?>>(propertyExpression, parameters);
                return order == SortOrder.Ascending ? entities.OrderBy(newExpression) : entities.OrderByDescending(newExpression);
            }

            if (propertyExpression.Type == typeof(bool))
            {
                var newExpression = Expression.Lambda<Func<T, bool>>(propertyExpression, parameters);
                return order == SortOrder.Ascending ? entities.OrderBy(newExpression) : entities.OrderByDescending(newExpression);
            }

            throw new NotSupportedException("Object type resolution not implemented for this type");
        }
        if(entities.GetType().IsAssignableFrom(typeof(IOrderedQueryable<T>)))
                return order == SortOrder.Ascending ? (entities as IOrderedQueryable<T>).ThenBy(expression) : (entities as IOrderedQueryable<T>).ThenByDescending(expression);

        return order == SortOrder.Ascending ? entities.OrderBy(expression) : entities.OrderByDescending(expression);
    }

我还没有测试上面的代码片段。

于 2012-05-31T14:49:58.613 回答