public MobileServiceCollection(IMobileServiceTableQuery <TTable> query, Func <IEnumerable <TTable>, IEnumerable <TCollection> > selector, int pageSize = 0)
        {
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }
            if (selector == null)
            {
                throw new ArgumentNullException("selector");
            }
            if (pageSize < 0)
            {
                throw new ArgumentOutOfRangeException("pageSize");
            }

            this.query = query;
            MobileServiceTableQuery <TTable> tableQuery = query as MobileServiceTableQuery <TTable>;

            if (tableQuery != null)
            {
                tableQuery.QueryProvider.Features = MobileServiceFeatures.TableCollection;
            }

            this.selectorFunction = selector;
            this.pageSize         = pageSize;

            this.HasMoreItems = true;
        }
        /// <summary>
        /// Create a new query based off an existing query and and a new
        /// queryable.  This is used via MobileServiceTableQueryable's
        /// combinators to construct new queries from simpler base queries.
        /// </summary>
        /// <param name="baseQuery">The base query.</param>
        /// <param name="query">The new queryable.</param>
        /// <param name="includeTotalCount">
        /// A value that if set will determine whether the query will request
        /// the total count for all the records that would have been returned
        /// ignoring any take paging/limit clause specified by client or
        /// server.  If this value is not set, we'll use the baseQuery's
        /// RequestTotalProperty instead (this is specifically so that our
        /// IncludeTotalCount method will preserve state on the old query).
        /// </param>
        /// <param name="parameters">
        /// The optional user-defined query string parameters to include with the query.
        /// </param>
        /// <returns>The new query.</returns>
        internal static MobileServiceTableQuery <T> Create(MobileServiceTableQuery <T> baseQuery, IQueryable <T> query, bool?includeTotalCount = null, IDictionary <string, string> parameters = null)
        {
            Debug.Assert(baseQuery != null, "baseQuery cannot be null!");
            Debug.Assert(query != null, "query cannot be null!");

            // Merge parameters if the baseQuery already has a collection of parameters
            if (baseQuery.Parameters != null)
            {
                if (parameters == null)
                {
                    parameters = baseQuery.Parameters;
                }
                else
                {
                    parameters = new Dictionary <string, string>(parameters);
                    foreach (var parameter in baseQuery.Parameters)
                    {
                        parameters.Add(parameter);
                    }
                }
            }

            // NOTE: Make sure any changes to this logic are reflected in the
            // Select method below which has its own version of this code to
            // work around type changes for its projection.
            return(new MobileServiceTableQuery <T>(
                       baseQuery.Table,
                       includeTotalCount ?? baseQuery.RequestTotalCount,
                       parameters)
            {
                Query = query
            });
        }
        /// <summary>
        /// Execute a query and return its results.
        /// </summary>
        /// <typeparam name="T">
        /// The type of element returned by the query.
        /// </typeparam>
        /// <param name="query">
        /// The query to evaluate and get the results for.
        /// </param>
        /// <returns>
        /// Results of the query.
        /// </returns>
        internal async Task <IEnumerable <T> > Execute <T>(MobileServiceTableQuery <T> query)
        {
            // Compile the query from the underlying IQueryable's expression
            // tree
            MobileServiceTableQueryDescription compiledQuery = this.Compile(query);

            // Send the query
            string odata    = compiledQuery.ToQueryString();
            JToken response = await query.Table.ReadAsync(odata, query.Parameters);

            // Parse the results
            long   totalCount;
            JArray values = this.GetResponseSequence(response, out totalCount);

            return(new TotalCountEnumerable <T>(
                       totalCount,
                       query.Table.MobileServiceClient.Serializer.Deserialize(values, compiledQuery.ProjectionArgumentType).Select(
                           value =>
            {
                // Apply the projection to the instance transforming it
                // as desired
                foreach (Delegate projection in compiledQuery.Projections)
                {
                    value = projection.DynamicInvoke(value);
                }

                return (T)value;
            })));
        }
Пример #4
0
        /// <summary>
        /// Initializes a new instance of the MobileServiceTableQueryTranslator
        /// class.
        /// </summary>
        /// <param name="query">
        /// The <see cref="T:MobileServiceTableQuery`1{T}"/> which
        /// is being translated.
        /// </param>
        internal MobileServiceTableQueryTranslator(MobileServiceTableQuery <T> query)
        {
            Debug.Assert(query != null);

            this.query = query;

            this.queryDescription = new MobileServiceTableQueryDescription(query.Table.TableName, query.RequestTotalCount);
        }
Пример #5
0
        public Task <IEnumerable <U> > ReadAsync <U>(MobileServiceTableQuery <U> query)
        {
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }

            return(EvaluateQueryAsync <U>(this, query.Compile()));
        }
        /// <summary>
        /// Compile the query into a MobileServiceTableQueryDescription.
        /// </summary>
        /// <returns>
        /// The compiled OData query.
        /// </returns>
        internal MobileServiceTableQueryDescription Compile <T>(MobileServiceTableQuery <T> query)
        {
            // Compile the query from the underlying IQueryable's expression
            // tree
            MobileServiceTableQueryTranslator <T> translator    = new MobileServiceTableQueryTranslator <T>(query);
            MobileServiceTableQueryDescription    compiledQuery = translator.Translate();

            return(compiledQuery);
        }
Пример #7
0
        /// <summary>
        /// Create a new query based off an existing query and and a new
        /// queryable.  This is used via MobileServiceTableQueryable's
        /// combinators to construct new queries from simpler base queries.
        /// </summary>
        /// <param name="baseQuery">The base query.</param>
        /// <param name="query">The new queryable.</param>
        /// <param name="includeTotalCount">
        /// A value that if set will determine whether the query will request
        /// the total count for all the records that would have been returned
        /// ignoring any take paging/limit clause specified by client or
        /// server.  If this value is not set, we'll use the baseQuery's
        /// RequestTotalProperty instead (this is specifically so that our
        /// IncludeTotalCount method will preserve state on the old query).
        /// </param>
        /// <returns>The new query.</returns>
        internal static MobileServiceTableQuery <T> Create(MobileServiceTableQuery <T> baseQuery, IQueryable <T> query, bool?includeTotalCount = null)
        {
            Debug.Assert(baseQuery != null, "baseQuery cannot be null!");
            Debug.Assert(query != null, "query cannot be null!");

            // NOTE: Make sure any changes to this logic are reflected in the
            // Select method below which has its own version of this code to
            // work around type changes for its projection.
            return(new MobileServiceTableQuery <T>(
                       baseQuery.Table,
                       includeTotalCount ?? baseQuery.RequestTotalCount)
            {
                Query = query
            });
        }