Query Filter

Download Samples Repository

Description

This sample demonstrates how to apply a filter to a feature layer using the FeatureLayer.Where property and to further filter the result set using a QueryFilter. The FeatureLayer.Where property allows you to retrieve a subset of records from a feature service that match an attribute query.The ArcGISFeatureTable.QueryAsync method takes a QueryFilter object and uses it to filter the current feature set.In this example, the features returned by the query are converted to graphics and displayed in a GraphicsOverlay on the map.

"Desktop" "Store" "Phone" Available for Desktop, Store, Phone

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.QueryFilterSample"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013">
    <Grid>
        <Grid.Resources>
            <esri:SimpleRenderer x:Key="OrangePointRenderer">
                <esri:SimpleRenderer.Symbol>
                    <esri:SimpleMarkerSymbol Color="Orange" Style="Circle" Size="14">
                        <esri:SimpleMarkerSymbol.Outline>
                            <esri:SimpleLineSymbol Color="Black" Width="1" Style="Solid" />
                        </esri:SimpleMarkerSymbol.Outline>
                    </esri:SimpleMarkerSymbol>
                </esri:SimpleRenderer.Symbol>
            </esri:SimpleRenderer>

            <esri:SimpleRenderer x:Key="BluePointRenderer">
                <esri:SimpleRenderer.Symbol>
                    <esri:SimpleMarkerSymbol Color="Blue" Style="Circle" Size="14">
                        <esri:SimpleMarkerSymbol.Outline>
                            <esri:SimpleLineSymbol Color="Black" Width="1" Style="Solid" />
                        </esri:SimpleMarkerSymbol.Outline>
                    </esri:SimpleMarkerSymbol>
                </esri:SimpleRenderer.Symbol>
            </esri:SimpleRenderer>
        </Grid.Resources>

		<esri:MapView x:Name="MyMapView" WrapAround="True" >
			<esri:Map InitialViewpoint="-14675766,2695408,-6733122,6583994">
				<esri:ArcGISTiledMapServiceLayer
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
				<esri:FeatureLayer x:Name="cities" ID="featureLayer" 
								   Renderer="{StaticResource OrangePointRenderer}">
					<esri:FeatureLayer.FeatureTable>
						<esri:ServiceFeatureTable
                            ServiceUri="http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/0"
                            Where="pop2000 > 200000" OutFields="*" />
					</esri:FeatureLayer.FeatureTable>
				</esri:FeatureLayer>
            </esri:Map>
			<esri:MapView.GraphicsOverlays>
				<esri:GraphicsOverlay ID="queryResults" Renderer="{StaticResource BluePointRenderer}" />
			</esri:MapView.GraphicsOverlays>
        </esri:MapView>

        <Border Background="White" BorderBrush="Black" BorderThickness="1"
				HorizontalAlignment="Right" VerticalAlignment="Top" 
				Margin="30" Padding="20">
            <StackPanel>
                <TextBlock 
					Text="Query cities" 
                    FontSize="14" FontWeight="Bold" HorizontalAlignment="Center" />
                <StackPanel Orientation="Horizontal" Margin="5" HorizontalAlignment="Center">
                    <TextBox x:Name="where" Margin="5" Text="pop2000 > 500000" Width="200"/>
                    <Button Content="Query" Click="QueryButton_Click" Margin="5"/>
                </StackPanel>
            </StackPanel>
        </Border>
    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Layers;
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
    /// <summary>
    /// This sample demonstrates how to apply a filter to a feature layer using the FeatureLayer.Where property and to further filter the result set using a QueryFilter. The FeatureLayer.Where property allows you to retrieve a subset of records from a feature service that match an attribute query.  The ArcGISFeatureTable.QueryAsync method takes a QueryFilter object and uses it to filter the current feature set.  In this example, the features returned by the query are converted to graphics and displayed in a GraphicsOverlay on the map.
    /// </summary>
    /// <title>Query Filter</title>
	/// <category>Layers</category>
	/// <subcategory>Feature Layers</subcategory>
	public partial class QueryFilterSample : UserControl
    {
        /// <summary>Construct QueryFilterSample control</summary>
        public QueryFilterSample()
        {
            InitializeComponent();
        }

        private async void QueryButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                var features = await cities.FeatureTable.QueryAsync(new QueryFilter() { WhereClause = where.Text});
				MyMapView.GraphicsOverlays["queryResults"].GraphicsSource = features.Select(f => new Graphic(f.Geometry));
            }
            catch (Exception ex)
            {
                MessageBox.Show("Query Error: " + ex.Message, "Query Filter Sample");
            }
        }
    }
}
Feedback on this topic?