// Execute a statistical query using the parameters defined by the user and display the results private async void OnExecuteStatisticsQueryClicked(object sender, EventArgs e) { // Verify that there is at least one statistic definition if (!_statDefinitions.Any()) { await((Page)Parent).DisplayAlert("Please define at least one statistic for the query.", "Statistical Query", "OK"); return; } // Create the statistics query parameters, pass in the list of statistic definitions StatisticsQueryParameters statQueryParams = new StatisticsQueryParameters(_statDefinitions); // Specify the group fields (if any) foreach (string groupField in _groupByFields) { statQueryParams.GroupByFieldNames.Add(groupField); } // Specify the fields to order by (if any) foreach (OrderFieldOption orderBy in _orderByFields) { statQueryParams.OrderByFields.Add(orderBy.OrderInfo); } // Execute the statistical query with these parameters and await the results StatisticsQueryResult statQueryResult = await _usStatesTable.QueryStatisticsAsync(statQueryParams); // Get results formatted as a lookup (list of group names and their associated dictionary of results) ILookup <string, IReadOnlyDictionary <string, object> > resultsLookup = statQueryResult.ToLookup(result => string.Join(", ", result.Group.Values), result => result.Statistics); // Loop through the formatted results and build a list of classes to display as grouped results in the list view ObservableCollection <ResultGroup> resultsGroupCollection = new ObservableCollection <ResultGroup>(); foreach (IGrouping <string, IReadOnlyDictionary <string, object> > group in resultsLookup) { // Create a new group ResultGroup resultGroup = new ResultGroup() { GroupName = group.Key }; // Loop through all the results for this group and add them to the collection foreach (IReadOnlyDictionary <string, object> resultSet in group) { foreach (KeyValuePair <string, object> result in resultSet) { resultGroup.Add(new StatisticResult { FieldName = result.Key, StatValue = result.Value }); } } // Add the group of results to the collection resultsGroupCollection.Add(resultGroup); } // Apply the results to the list view data source and show the results grid StatResultsList.ItemsSource = resultsGroupCollection; ResultsGrid.IsVisible = true; }
// Execute a statistical query using the parameters defined by the user and display the results private async void OnExecuteStatisticsQueryClicked(object sender, RoutedEventArgs e) { // Verify that there is at least one statistic definition if (_statDefinitions.Count == 0) { MessageBox.Show("Please define at least one statistic for the query.", "Statistical Query"); return; } // Create the statistics query parameters, pass in the list of statistic definitions StatisticsQueryParameters statQueryParams = new StatisticsQueryParameters(_statDefinitions); // Specify the group fields (if any) foreach (string groupField in _groupByFields) { statQueryParams.GroupByFieldNames.Add(groupField); } // Specify the fields to order by (if any) foreach (OrderBy orderBy in _orderByFields) { statQueryParams.OrderByFields.Add(orderBy); } // Ignore counties with missing data statQueryParams.WhereClause = "\"State\" IS NOT NULL"; try { // Execute the statistical query with these parameters and await the results StatisticsQueryResult statQueryResult = await _usStatesTable.QueryStatisticsAsync(statQueryParams); // Format the output, and display results in the tree view ILookup <string, IReadOnlyDictionary <string, object> > groupedResults = statQueryResult.ToLookup(r => string.Join(", ", r.Group.Values), r => r.Statistics); ResultsTreeView.ItemsSource = groupedResults; } catch (Exception ex) { MessageBox.Show(ex.Message, "Invalid statistics definitions"); } }