Пример #1
0
        private static void AppendQueryStringParams(IAsyncDocumentQuery <dynamic> query, IEnumerable <KeyValuePair <string, string> > queryParams)
        {
            queryParams.ForEach(q =>
            {
                switch (q.Key.ToLowerInvariant())
                {
                case "$top":
                    int pageSize;
                    if (int.TryParse(q.Value, out pageSize))
                    {
                        query.Take(pageSize);
                    }
                    break;

                case "$skip":
                    int skipCount;
                    if (int.TryParse(q.Value, out skipCount))
                    {
                        query.Skip(skipCount);
                    }
                    break;

                case "$filter":
                    query.Where(q.Value);
                    break;
                }
            });
        }
        public static IAsyncDocumentQuery <T> FilterByStatusWhere <T>(this IAsyncDocumentQuery <T> source, Request request)
        {
            string status = null;

            if ((bool)request.Query.status.HasValue)
            {
                status = (string)request.Query.status;
            }

            if (status == null)
            {
                return(source);
            }

            var filters  = status.Replace(" ", String.Empty).Split(',');
            var excludes = new List <int>();
            var includes = new List <int>();

            foreach (var filter in filters)
            {
                FailedMessageStatus failedMessageStatus;

                if (filter.StartsWith("-"))
                {
                    if (Enum.TryParse(filter.Substring(1), true, out failedMessageStatus))
                    {
                        excludes.Add((int)failedMessageStatus);
                    }
                    continue;
                }

                if (Enum.TryParse(filter, true, out failedMessageStatus))
                {
                    includes.Add((int)failedMessageStatus);
                }
            }

            var sb = new StringBuilder();

            sb.Append("((");
            if (includes.Count == 0)
            {
                sb.Append("*");
            }
            else
            {
                sb.Append(String.Join(" OR ", includes.ToArray()));
            }
            sb.Append(")");

            if (excludes.Count > 0)
            {
                sb.Append(" AND NOT (");
                sb.Append(String.Join(" OR ", excludes.ToArray()));
                sb.Append(")");
            }
            sb.Append(")");

            source.AndAlso();
            source.Where($"Status: {sb}");

            return(source);
        }