示例#1
0
        /// <summary>
        /// Tries to parse the string and returns a list of <see cref="OrderByExpression"/>.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="expressions">The expressions.</param>
        /// <returns>
        /// <c>true</c> if the source was parsed into a list of <see cref="OrderByExpression"/>; otherwise,
        /// <c>false</c>.
        /// </returns>
        public static bool TryParseOrderByExpressions(this string source, out OrderByExpression[] expressions)
        {
            expressions = new OrderByExpression[] { };

            if (!string.IsNullOrWhiteSpace(source))
            {
                var orderByPattern = @"[a-z0-9]+(\s+(asc|desc))?";
                var pattern = string.Format(@"^\s*{0}\s*(,\s*{0}\s*)*$", orderByPattern);

                if (!Regex.IsMatch(source, pattern, RegexOptions.IgnoreCase))
                {
                    return false;
                }

                var expressionList = new List<OrderByExpression>();

                foreach (var expressionToken in Regex.Replace(source, @"\s+", " ").Split(','))
                {
                    var expressionParts = expressionToken.Trim().Split(' ');
                    var sortDirection = SortDirection.Ascending;

                    if (expressionParts.Length == 2 &&
                        expressionParts[1].Equals("desc", StringComparison.CurrentCultureIgnoreCase))
                    {
                        sortDirection = SortDirection.Descending;
                    }

                    expressionList.Add(new OrderByExpression(expressionParts[0], sortDirection));
                }

                expressions = expressionList.ToArray();
            }

            return true;
        }
        public void CreateIsProperlyInitialized()
        {
            var expression = new OrderByExpression("Property", SortDirection.Descending);

            Assert.Equal("Property", expression.PropertyName);
            Assert.Equal(SortDirection.Descending, expression.Direction);
        }
示例#3
0

        
示例#4
0
        /// <summary>
        /// Tries to parse the string and returns a list of <see cref="OrderByExpression"/>.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="expressions">The expressions.</param>
        /// <returns>
        /// <c>true</c> if the source was parsed into a list of <see cref="OrderByExpression"/>; otherwise,
        /// <c>false</c>.
        /// </returns>
        public static bool TryParseOrderByExpressions(this string source, out OrderByExpression[] expressions)
        {
            expressions = new OrderByExpression[] { };

            if (!string.IsNullOrWhiteSpace(source))
            {
                var orderByPattern = @"[a-z0-9]+(\s+(asc|desc))?";
                var pattern        = string.Format(@"^\s*{0}\s*(,\s*{0}\s*)*$", orderByPattern);

                if (!Regex.IsMatch(source, pattern, RegexOptions.IgnoreCase))
                {
                    return(false);
                }

                var expressionList = new List <OrderByExpression>();

                foreach (var expressionToken in Regex.Replace(source, @"\s+", " ").Split(','))
                {
                    var expressionParts = expressionToken.Trim().Split(' ');
                    var sortDirection   = SortDirection.Ascending;

                    if (expressionParts.Length == 2 &&
                        expressionParts[1].Equals("desc", StringComparison.CurrentCultureIgnoreCase))
                    {
                        sortDirection = SortDirection.Descending;
                    }

                    expressionList.Add(new OrderByExpression(expressionParts[0], sortDirection));
                }

                expressions = expressionList.ToArray();
            }

            return(true);
        }
        public void TryParseOrderByExpressionsWithValidSourceReturnsExpressions(
            string orderBy, OrderByExpression[] expectedExpressions)
        {
            OrderByExpression[] expressions = null;

            var result = orderBy.TryParseOrderByExpressions(out expressions);

            Assert.True(result);

            var comparer = new DelegateEqualityComparer<OrderByExpression>(
                (OrderByExpression x, OrderByExpression y) =>
                {
                    return x.PropertyName == y.PropertyName && x.Direction == y.Direction;
                },
                (OrderByExpression obj) => { return obj.GetHashCode(); });

            Assert.Equal(expectedExpressions, expressions, comparer);
        }