Beispiel #1
0
        private void HandleOrderByExpression(ReadOnlyCollection <Expression> arguments, SortDirectionConstants sortDir)
        {
            if (arguments.Count == 2)
            {
                var unaryExpr = arguments[1] as UnaryExpression;
                if (unaryExpr != null)
                {
                    var fieldSort = new FieldSort();
                    fieldSort.Name          = ExpressionHelper.GetMemberName(unaryExpr.Operand);
                    fieldSort.SortDirection = sortDir;

                    //Do not throw errow for now, just week it out
                    //if (this.Query.FieldSorts.FindIndex(x => x.Name == fieldSort.Name) >= 0)
                    //    throw new InvalidOperationException($"Duplicate sort field \"{fieldSort.Name}\" was encountered.");

                    //Only add if not exists
                    if (this.Query.FieldSorts.FindIndex(x => x.Name == fieldSort.Name) == -1)
                    {
                        this.Query.FieldSorts.Add(fieldSort);
                    }

                    return; // Success
                }

                var constantExpr = arguments[1] as ConstantExpression;
                if (constantExpr != null)
                {
                    var fieldSort = new FieldSort();
                    fieldSort.Name          = (string)constantExpr.Value;
                    fieldSort.SortDirection = sortDir;

                    this.Query.FieldSorts.RemoveAll(x => x.Name == fieldSort.Name);
                    this.Query.FieldSorts.Add(fieldSort);
                    return; // Success
                }

                throw new InvalidOperationException("Invalid OrderBy expression arguments.");
            }
        }
 public static string ToSqlDirection(this SortDirectionConstants sort)
 {
     return(sort == SortDirectionConstants.Desc ? "DESC" : "ASC");
 }