Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for .NET

Statistical query group and sort

This code sample is available for these platforms:
View Sample on GitHub

Query a feature table for statistics, grouping and sorting by different fields.

Image of statistical query group and sort

Use case

You can use statistical queries, grouping and sorting to process large amounts of data saved in feature tables. This is helpful for identifying trends and relationships within the data, which can be used to support further interpretations and decisions. For example, a health agency can use information on medical conditions occurring throughout a country to identify at-risk areas or demographics, and decide on further action and preventive measures.

How to use the sample

The sample will start with some default options selected. You can immediately tap the "Get Statistics" button to see the results for these options. There are several ways to customize your queries:

  • You can add statistic definitions to the top-left table using the combo boxes and "Add" button. Select a table row and tap "Remove" to remove a definition.
  • To change the Group-by fields, check the box by the field you want to group by in the bottom-left list view.
  • To change the Order-by fields, select a Group-by field (it must be checked) and tap the ">>" button to add it to the Order-by table. To remove a field from the Order-by table, select it and tap the "<<" button. To change the sort order of the Order-by field, the cells of the "Sort Order" column are combo-boxes that may be either ASCENDING or DESCENDING.

How it works

  1. Create a ServiceFeatureTable using the URL of a feature service and load the table.
  2. Get the feature tables field names list with featureTable.Fields.
  3. Create StatisticDefinitions specifying the field to compute statistics on and the StatisticType to compute.
  4. Create StatisticsQueryParameters passing in the list of statistic definitions.
  5. To have the results grouped by fields, add the field names to the query parameters' GroupByFieldNames collection.
  6. To have the results ordered by fields, create OrderBys, specifying the field name and SortOrder. Pass these OrderBys to the parameters' OrderByFields collection.
  7. To execute the query, call featureTable.QueryStatisticsAsync(queryParameters).
  8. Get the StatisticQueryResult. From this, you can get an iterator of StatisticRecords to loop through and display.

Relevant API

  • Field
  • OrderBy
  • QueryParameters
  • ServiceFeatureTable
  • StatisticDefinition
  • StatisticRecord
  • StatisticsQueryParameters
  • StatisticsQueryResult
  • StatisticType

About the data

This sample uses a Diabetes, Obesity, and Inactivity by US County feature layer hosted on ArcGIS Online.

Tags

correlation, data, fields, filter, group, sort, statistics, table

Sample Code

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="ArcGISRuntime.Samples.StatsQueryGroupAndSort.StatsQueryGroupAndSort"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    <Grid HorizontalOptions="Center" VerticalOptions="Center">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="5" />
            <ColumnDefinition Width="120" />
            <ColumnDefinition Width="5" />
            <ColumnDefinition Width="120" />
            <ColumnDefinition Width="35" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="40" />
            <RowDefinition Height="auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="40" />
            <RowDefinition Height="40" />
            <RowDefinition Height="*" />
            <RowDefinition Height="30" />
            <RowDefinition Height="*" />
            <RowDefinition Height="30" />
            <RowDefinition Height="50" />
        </Grid.RowDefinitions>
        <Label
            Grid.Row="0"
            Grid.Column="1"
            Grid.ColumnSpan="3"
            FontSize="18"
            HorizontalOptions="Center"
            Text="Statistics: US States"
            TextColor="Blue"
            VerticalOptions="Center" />
        <Picker
            x:Name="FieldsComboBox"
            Title="Field"
            Grid.Row="1"
            Grid.Column="1"
            Margin="5"
            HorizontalOptions="Fill"
            VerticalOptions="CenterAndExpand" />
        <Picker
            x:Name="StatTypeComboBox"
            Title="Statistic"
            Grid.Row="1"
            Grid.Column="3"
            Margin="5"
            HorizontalOptions="Fill"
            VerticalOptions="CenterAndExpand" />
        <Button
            x:Name="AddStatisticButton"
            Grid.Row="1"
            Grid.Column="4"
            Margin="3"
            Clicked="AddStatisticClicked"
            HorizontalOptions="Start"
            Text="+" />
        <ListView
            x:Name="StatFieldsListBox"
            Grid.Row="2"
            Grid.ColumnSpan="5"
            Margin="25,5"
            BackgroundColor="Gainsboro"
            HeightRequest="120">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.View>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition />
                                    <ColumnDefinition />
                                </Grid.ColumnDefinitions>
                                <Label Text="{Binding OnFieldName}" />
                                <Label Grid.Column="1" Text="{Binding StatisticType}" />
                            </Grid>
                        </ViewCell.View>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <Button
            x:Name="RemoveStatField"
            Grid.Row="3"
            Grid.Column="3"
            Clicked="RemoveStatisticClicked"
            HeightRequest="30"
            HorizontalOptions="Fill"
            Text="Remove"
            VerticalOptions="Start" />
        <Label
            Grid.Row="4"
            Grid.ColumnSpan="2"
            Margin="25,0,0,5"
            HorizontalOptions="Start"
            Text="Group by"
            VerticalOptions="End" />
        <ListView
            x:Name="GroupFieldsListBox"
            Grid.Row="5"
            Grid.ColumnSpan="5"
            Margin="25,0"
            BackgroundColor="Gainsboro"
            HorizontalOptions="Fill"
            MinimumHeightRequest="110"
            VerticalOptions="Fill">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.View>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition />
                                    <ColumnDefinition />
                                </Grid.ColumnDefinitions>
                                <Switch Toggled="GroupFieldCheckChanged" />
                                <Label
                                    Grid.Column="1"
                                    Text="{Binding Name}"
                                    VerticalOptions="Center" />
                            </Grid>
                        </ViewCell.View>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <Label
            Grid.Row="6"
            Grid.ColumnSpan="2"
            Margin="25,0,0,5"
            HorizontalOptions="Start"
            Text="Order by"
            VerticalOptions="End" />
        <ListView
            x:Name="OrderByFieldsListBox"
            Grid.Row="7"
            Grid.ColumnSpan="5"
            Margin="25,0"
            BackgroundColor="Gainsboro"
            HorizontalOptions="Fill"
            VerticalOptions="Fill">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.View>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition />
                                    <ColumnDefinition Width="2*" />
                                    <ColumnDefinition Width="2*" />
                                </Grid.ColumnDefinitions>
                                <Switch IsToggled="{Binding OrderWith}" />
                                <Label
                                    Grid.Column="1"
                                    Text="{Binding OrderInfo.FieldName}"
                                    VerticalOptions="Center" />
                                <Label
                                    Grid.Column="2"
                                    Text="{Binding OrderInfo.SortOrder}"
                                    VerticalOptions="Center" />
                            </Grid>
                        </ViewCell.View>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <Button
            x:Name="SortOrderButton"
            Grid.Row="8"
            Grid.Column="3"
            Grid.ColumnSpan="2"
            Margin="0,2,25,0"
            Clicked="ChangeFieldSortOrder"
            FontSize="12"
            HorizontalOptions="Center"
            Text="Change sort order"
            VerticalOptions="Start" />
        <Button
            x:Name="GetStatisticsButton"
            Grid.Row="9"
            Grid.Column="0"
            Grid.ColumnSpan="5"
            Margin="25,5,30,5"
            Clicked="OnExecuteStatisticsQueryClicked"
            HeightRequest="40"
            HorizontalOptions="Fill"
            Text="Get Statistics"
            VerticalOptions="Fill" />
        <Grid
            x:Name="ResultsGrid"
            Grid.Row="1"
            Grid.RowSpan="9"
            Grid.Column="0"
            Grid.ColumnSpan="5"
            BackgroundColor="Gainsboro"
            IsVisible="False">
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition Height="50" />
            </Grid.RowDefinitions>
            <ListView x:Name="StatResultsList" IsGroupingEnabled="true">
                <ListView.GroupHeaderTemplate>
                    <DataTemplate>
                        <TextCell Height="30" Text="{Binding GroupName}" />
                    </DataTemplate>
                </ListView.GroupHeaderTemplate>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextCell Detail="{Binding StatValue}" Text="{Binding FieldName}" />
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
            <Button
                Grid.Row="1"
                Margin="5"
                Clicked="HideResults"
                Text="Dismiss" />
        </Grid>
    </Grid>
</ContentPage>
// Copyright 2017 Esri.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.

using Esri.ArcGISRuntime.Data;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Xamarin.Forms;

namespace ArcGISRuntime.Samples.StatsQueryGroupAndSort
{
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        name: "Statistical query group and sort",
        category: "Data",
        description: "Query a feature table for statistics, grouping and sorting by different fields.",
        instructions: "The sample will start with some default options selected. You can immediately tap the \"Get Statistics\" button to see the results for these options. There are several ways to customize your queries:",
        tags: new[] { "correlation", "data", "fields", "filter", "group", "sort", "statistics", "table" })]
    public partial class StatsQueryGroupAndSort : ContentPage
    {
        // URI for the US states map service
        private Uri _usStatesServiceUri = new Uri("https://services.arcgis.com/jIL9msH9OI208GCb/arcgis/rest/services/Counties_Obesity_Inactivity_Diabetes_2013/FeatureServer/0");

        // US states feature table
        private FeatureTable _usStatesTable;

        // Collection of (user-defined) statistics to use in the query
        private ObservableCollection<StatisticDefinition> _statDefinitions = new ObservableCollection<StatisticDefinition>();

        // Selected fields for grouping results
        private List<string> _groupByFields = new List<string>();

        // Collection to hold fields to order results by
        private ObservableCollection<OrderFieldOption> _orderByFields = new ObservableCollection<OrderFieldOption>();

        public StatsQueryGroupAndSort()
        {
            InitializeComponent();

            // Initialize the US states feature table and populate UI controls
            Initialize();
        }

        private async void Initialize()
        {
            // Create the US states feature table
            _usStatesTable = new ServiceFeatureTable(_usStatesServiceUri);

            try
            {
                // Load the table
                await _usStatesTable.LoadAsync();

                // Fill the fields combo and "group by" list with field names from the table
                List<string> fieldNames = _usStatesTable.Fields.Select(field => field.Name).ToList();
                FieldsComboBox.ItemsSource = fieldNames;
                GroupFieldsListBox.ItemsSource = _usStatesTable.Fields;

                // Set the (initially empty) collection of fields as the "order by" fields list data source
                OrderByFieldsListBox.ItemsSource = _orderByFields;

                // Fill the statistics type combo with values from the StatisticType enum
                StatTypeComboBox.ItemsSource = Enum.GetValues(typeof(StatisticType));

                // Set the (initially empty) collection of statistic definitions as the statistics list box data source
                StatFieldsListBox.ItemsSource = _statDefinitions;
            }
            catch (Exception e)
            {
                await Application.Current.MainPage.DisplayAlert("Error", e.ToString(), "OK");
            }
        }

        // 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 Application.Current.MainPage.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);
            }

            // 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);

                // 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;
            }
            catch (Exception ex)
            {
                await Application.Current.MainPage.DisplayAlert("Error", ex.ToString(), "OK");
            }
        }        

        // Handle when the switch for a "group by" field is toggled on or off by adding or removing the field from the collection
        private void GroupFieldCheckChanged(object sender, EventArgs e)
        {
            // Get the check box that raised the event (group field)
            Switch groupFieldCheckBox = (Switch)sender;

            // Get the field name
            string fieldName = groupFieldCheckBox.BindingContext.ToString();

            // See if the field is being added or removed from the "group by" list
            bool fieldAdded = groupFieldCheckBox.IsToggled;

            // See if the field already exists in the "group by" list
            bool fieldIsInList = _groupByFields.Contains(fieldName);

            // If the field is being added, and is NOT in the list, add it ...
            if (fieldAdded && !fieldIsInList)
            {
                _groupByFields.Add(fieldName);

                // Also add it to the "order by" list
                OrderBy orderBy = new OrderBy(fieldName, SortOrder.Ascending);
                OrderFieldOption orderOption = new OrderFieldOption(false, orderBy);
                _orderByFields.Add(orderOption);
            }
            // If the field is being removed and it IS in the list, remove it ...
            else if (!fieldAdded && fieldIsInList)
            {
                _groupByFields.Remove(fieldName);

                // Also check for this field in the "order by" list and remove if necessary (only group fields can be used to order results)
                OrderFieldOption orderBy = _orderByFields.FirstOrDefault(field => field.OrderInfo.FieldName == fieldName);
                if (orderBy != null)
                {
                    // Remove the field from the "order by" list
                    _orderByFields.Remove(orderBy);
                }
            }
        }

        // Create a statistic definition and add it to the collection based on the user selection in the combo boxes
        private void AddStatisticClicked(object sender, EventArgs e)
        {
            // Verify that a field name and statistic type has been selected
            if (FieldsComboBox.SelectedItem == null || StatTypeComboBox.SelectedItem == null) { return; }

            // Get the chosen field name and statistic type from the combo boxes
            string fieldName = FieldsComboBox.SelectedItem.ToString();
            StatisticType statType = (StatisticType)StatTypeComboBox.SelectedItem;

            // Check if this statistic definition has already be created (same field name and statistic type)
            StatisticDefinition existingStatDefinition = _statDefinitions.FirstOrDefault(def => def.OnFieldName == fieldName && def.StatisticType == statType);

            // If it doesn't exist, create it and add it to the collection (use the field name and statistic type to build the output alias)
            if (existingStatDefinition == null)
            {
                StatisticDefinition statDefinition = new StatisticDefinition(fieldName, statType, fieldName + "_" + statType.ToString());
                _statDefinitions.Add(statDefinition);
            }
        }

        // Toggle the sort order (ascending/descending) for the field selected in the sort fields list
        private void ChangeFieldSortOrder(object sender, EventArgs e)
        {
            // Verify that there is a selected sort field in the list
            OrderFieldOption selectedSortField = OrderByFieldsListBox.SelectedItem as OrderFieldOption;
            if (selectedSortField == null) { return; }

            // Create a new order field info to define the sort for the selected field
            OrderBy newOrderBy = new OrderBy(selectedSortField.OrderInfo.FieldName, selectedSortField.OrderInfo.SortOrder);
            OrderFieldOption newSortDefinition = new OrderFieldOption(true, newOrderBy);

            // Toggle the sort order from the current value
            if (newSortDefinition.OrderInfo.SortOrder == SortOrder.Ascending)
            {
                newSortDefinition.OrderInfo.SortOrder = SortOrder.Descending;
            }
            else
            {
                newSortDefinition.OrderInfo.SortOrder = SortOrder.Ascending;
            }

            // Add the new OrderBy at the same location in the collection and remove the old one
            _orderByFields.Insert(_orderByFields.IndexOf(selectedSortField), newSortDefinition);
            _orderByFields.Remove(selectedSortField);
        }

        // Remove the selected statistic definition from the list
        private void RemoveStatisticClicked(object sender, EventArgs e)
        {
            // Verify that there is a selected statistic definition
            if (StatFieldsListBox.SelectedItem == null) { return; }

            // Get the selected statistic definition and remove it from the collection
            StatisticDefinition selectedStat = StatFieldsListBox.SelectedItem as StatisticDefinition;
            _statDefinitions.Remove(selectedStat);
        }

        // Add the selected field in the "group by" list to the "order by" list
        private void AddSortFieldClicked(object sender, EventArgs e)
        {
            // Verify that there is a selected field in the "group by" list
            if (GroupFieldsListBox.SelectedItem == null) { return; }

            // Get the name of the selected field and ensure that it's in the list of selected group fields (checked on in the list, e.g.)
            string selectedFieldName = GroupFieldsListBox.SelectedItem.ToString();
            if (!_groupByFields.Contains(selectedFieldName))
            {
                Application.Current.MainPage.DisplayAlert("Only fields used for grouping can be used to order results.", "Query", "OK");
                return;
            }

            // Verify that the field isn't already in the "order by" list
            OrderFieldOption existingOrderBy = _orderByFields.FirstOrDefault(field => field.OrderInfo.FieldName == selectedFieldName);
            if (existingOrderBy == null)
            {
                // Create a new OrderBy for this field and add it to the collection (default to ascending sort order)
                OrderBy newOrderBy = new OrderBy(selectedFieldName, SortOrder.Ascending);
                OrderFieldOption orderField = new OrderFieldOption(false, newOrderBy);
                _orderByFields.Add(orderField);
            }
        }

        // Remove the selected field from the list of "order by" fields
        private void RemoveSortFieldClicked(object sender, EventArgs e)
        {
            // Verify that there is a selected item in the "order by" list
            if (OrderByFieldsListBox.SelectedItem == null) { return; }

            // Get the selected OrderFieldOption object and remove it from the collection
            OrderFieldOption selectedOrderBy = OrderByFieldsListBox.SelectedItem as OrderFieldOption;
            _orderByFields.Remove(selectedOrderBy);
        }

        private void HideResults(object sender, EventArgs e)
        {
            // Dismiss the results grid and show the input controls
            ResultsGrid.IsVisible = false;
        }
    }

    // Simple class to describe an "order by" option
    public class OrderFieldOption
    {
        // Whether or not to use this field to order results
        public bool OrderWith { get; set; }

        // The order by info: field name and sort order
        public OrderBy OrderInfo { get; set; }

        public OrderFieldOption(bool orderWith, OrderBy orderInfo)
        {
            OrderWith = orderWith;
            OrderInfo = orderInfo;
        }
    }

    // A simple class to hold a single statistic result
    public class StatisticResult
    {
        public string FieldName { get; set; }
        public object StatValue { get; set; }
    }

    // A class to represent a group of statistics results
    public class ResultGroup : ObservableCollection<StatisticResult>
    {
        public string GroupName { get; set; }
        public string ShortName { get; set; }
    }
}