Пример #1
0
        private static Expression GetThenByCall(this Expression expression, OrderByClause orderByClause)
        {
            const string ThenBy           = "ThenBy";
            const string ThenByDescending = "ThenByDescending";

            return(orderByClause.ThenBy == null
                ? GetMethodCall()
                : GetMethodCall().GetThenByCall(orderByClause.ThenBy));

            Expression GetMethodCall()
            {
                return(orderByClause.Expression switch
                {
                    CountNode countNode => expression.GetOrderByCountCall
                    (
                        countNode.GetPropertyPath(),
                        orderByClause.Direction == OrderByDirection.Ascending
                            ? ThenBy
                            : ThenByDescending
                    ),
                    SingleValuePropertyAccessNode propertyNode => expression.GetOrderByCall
                    (
                        propertyNode.GetPropertyPath(),
                        orderByClause.Direction == OrderByDirection.Ascending
                            ? ThenBy
                            : ThenByDescending
                    ),
                    _ => throw new ArgumentException($"Unsupported SingleValueNode value: {orderByClause.Expression.GetType()}"),
                });
            }
Пример #2
0
        private static Expression GetOrderByCall(this Expression expression, OrderByClause orderByClause)
        {
            const string OrderBy           = "OrderBy";
            const string OrderByDescending = "OrderByDescending";

            return(orderByClause.ThenBy == null
                ? GetMethodCall()
                : GetMethodCall().GetThenByCall(orderByClause.ThenBy));

            Expression GetMethodCall()
            {
                SingleValueNode orderByNode = orderByClause.Expression;

                switch (orderByNode)
                {
                case CountNode countNode:
                    return(expression.GetOrderByCountCall
                           (
                               countNode.GetPropertyPath(),
                               orderByClause.Direction == OrderByDirection.Ascending
                                ? OrderBy
                                : OrderByDescending,
                               orderByClause.RangeVariable.Name
                           ));

                default:
                    SingleValuePropertyAccessNode propertyNode = (SingleValuePropertyAccessNode)orderByNode;
                    return(expression.GetOrderByCall
                           (
                               propertyNode.GetPropertyPath(),
                               orderByClause.Direction == OrderByDirection.Ascending
                                ? OrderBy
                                : OrderByDescending,
                               orderByClause.RangeVariable.Name
                           ));
                }
            }
        }