/// <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; }))); }
/// <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); }
/// <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); }
/// <summary> /// Initializes a new instance of the MobileServiceCollectionView /// class. /// </summary> /// <param name="table"> /// Table associated with the data source's query. /// </param> /// <param name="query">The query the data source represents.</param> internal MobileServiceCollectionView(MobileServiceTable <T> table, MobileServiceTableQueryDescription query) { Debug.Assert(table != null, "table cannot be null!"); Debug.Assert(query != null, "query cannot be null!"); this.table = table; this.query = query; // Evaluate the query immediately and start loading the data for it this.data = new List <T>(); this.EvaluateQueryAsync(); }
/// <summary> /// Compile the query into a MobileServiceTableQueryDescription. /// </summary> /// <returns>The compiled OData query.</returns> internal MobileServiceTableQueryDescription Compile() { // Compile the query from the underlying IQueryable's expression // tree MobileServiceTableQueryDescription compiledQuery = MobileServiceTableQueryTranslator.Translate(this.Query.Expression); // Forward along the request for the total count compiledQuery.IncludeTotalCount = this.RequestTotalCount; // Associate the current table with the compiled query if (string.IsNullOrEmpty(compiledQuery.TableName)) { SerializableType type = SerializableType.Get( compiledQuery.ProjectionArgumentType ?? typeof(T)); compiledQuery.TableName = type.TableName; } return(compiledQuery); }
/// <summary> /// Evaluate a query and return its results. /// </summary> /// <typeparam name="U"> /// The type of element returned by the query. /// </typeparam> /// <param name="table"> /// Access to MobileServices table operations. /// </param> /// <param name="query"> /// The description of the query to evaluate and get the results for. /// </param> /// <returns>Results of the query.</returns> internal static Task <IEnumerable <U> > EvaluateQueryAsync <U>(IMobileServiceTable <T> table, MobileServiceTableQueryDescription query) { Debug.Assert(query != null, "query cannot be null!"); Debug.Assert(table != null, "table cannot be null!"); // Send the query string odata = query.ToString(); return(table.ReadAsync(odata, query.Parameters).ContinueWith(t => { // Parse the results long totalCount; JsonArray values = MobileServiceTable <T> .GetResponseSequence(t.Result, out totalCount); return (IEnumerable <U>) new TotalCountEnumerable <U>( totalCount, values.Select( item => { // Create and fill a new instance of the type we should // deserialize (which is either T or the input param // to the projection that will modify T). object obj = Activator.CreateInstance(query.ProjectionArgumentType ?? typeof(U)); MobileServiceTableSerializer.Deserialize(item, obj); // Apply the projection to the instance transforming it // as desired if (query.Projection != null) { obj = query.Projection.DynamicInvoke(obj); } return (U)obj; })); })); }
/// <summary> /// Create a new collection view based on the query. /// </summary> /// <returns>The collection view.</returns> public MobileServiceCollectionView <T> ToCollectionView() { MobileServiceTableQueryDescription query = this.Compile(); return(new MobileServiceCollectionView <T>(this.Table, query)); }