Query a feature layer (SQL)
Learn how to execute a SQL query to return features from a feature layer based on spatial and attribute criteria.
A feature layer can contain a large number of features stored in ArcGIS. You can query a layer to access a subset of its features using any combination of spatial and attribute criteria. You can control whether or not each feature's geometry is returned, as well as which attributes are included in the results. Queries allow you to return a well-defined subset of your hosted data for analysis or display in your app.
In this tutorial, you'll write code to perform SQL queries that return a subset of features in the LA County Parcel feature layer (containing over 2.4 million features). Features that meet the query criteria are selected in the map.
Prerequisites
Before starting this tutorial, you should:
- Have an ArcGIS account and an API key to access ArcGIS services. If you don't have an account, sign up for free.
- Ensure your development environment meets the system requirements.
Optionally, you may want to install the ArcGIS Maps SDK for .NET to get access to project templates in Visual Studio (Windows only) and offline copies of the NuGet packages.
Steps
Open a Visual Studio solution
To start the tutorial, complete the Display a map tutorial or download and unzip the solution.
Open the
.sln
file in Visual Studio.If you downloaded the solution project, set your API key.
An API Key enables access to services, web maps, and web scenes hosted in ArcGIS Online.
If necessary, set the API Key.Go to your developer dashboard to get your API key. For these tutorials, use your default API key. It is scoped to include all of the services demonstrated in the tutorials.
In Visual Studio, in the Solution Explorer, click App.xaml.cs.
In the App class, add an override for the
O
function to set then Startup() Api
property onKey ArcGISRuntimeEnvironment
.App.xaml.csUse dark colors for code blocks Add line. Add line. Add line. Add line. Add line. Add line. Add line.
If you are developing with Visual Studio for Windows, ArcGIS Maps SDK for .NET provides a set of project templates for each supported .NET platform. These templates follow the Model-View-ViewModel (MVVM) design pattern. Install the ArcGIS Maps SDK for .NET Visual Studio Extension to add the templates to Visual Studio (Windows only). See Install and set up for details.
Update the tutorial name used in the project (optional)
The Visual Studio solution, project, and the namespace for all classes currently use the name DisplayAMap. Follow the steps below if you prefer the name to reflect the current tutorial. These steps are not required, your code will still work if you keep the original name.
The tutorial instructions and code use the name QueryAFeatureLayerSQL for the solution, project, and namespace. You can choose any name you like, but it should be the same for each of these.
Update the name for the solution and the project.
- In Visual Studio, in the Solution Explorer, right-click the solution name and choose Rename. Provide the new name for your solution.
- In the Solution Explorer, right-click the project name and choose Rename. Provide the new name for your project.
Rename the namespace used by classes in the project.
- In the Solution Explorer, expand the project node.
- Double-click MapViewModel.cs in the Solution Explorer to open the file.
- In the
Map
class, double-click the namespace name (View Model Display
) to select it, and then right-click and choose Rename....AMap - Provide the new name for the namespace.
- Click Apply in the Rename: DisplayAMap window that appears in the upper-right of the code window. This will rename the namespace throughout your project.
Build the project.
- Choose Build > Build solution (or press <F6>).
Steps
Add UI for selecting a predefined query expression
To make performing a query on a feature layer more flexible, add a Combo
control to present a list of predefined attribute queries for the parcels dataset. While editing this file, also modify the Map
control to bind its Selection
to a property in the view model (so you can set the color used to display selected parcels).
In the Visual Studio > Solution Explorer, double-click the MainWindow.xaml file to open it.
Add XAML that defines a
Combo
control and its behavior.Box - The control is positioned above the map view in the upper-left corner.
- A list of
Combo
s shows predefined expression choices.B o x Item - The first combo box item instructs the user to choose an expression that will be selected when the app initializes.
- Changes in the selected expression will be handled by the
Query
event handler.Combo B o x_ Selection Changed
Also modify the
Map
control to bind theView Selection
property.Properties MainWindow.xamlUse dark colors for code blocks Change line Change line Change line Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. In the section for the combo box you just added, right-click on the name of the event handler text ("QueryComboBox_SelectionChanged") and choose Go To definition from the context menu. Visual Studio will open the MapWindows.xaml.cs file with the stub for the new
Query
function.Combo B o x_ Selection Changed()
Add code to call the selected query for the current map extent
When the user chooses an expression in the combo box, call a function to execute the query.
The function to execute the query takes three arguments: the ID for the layer to query (string), a SQL expression that defines attribute criteria (string), and the area of the MapView
currently being viewed (Envelope
).
In the
Query
function: modify the method signature to add theCombo B o x_ Selection Changed() async
keyword.MainWindow.xaml.csUse dark colors for code blocks Change line Change line Again, in the
Query
function: add code to gather the information required to perform the query, then call theCombo B o x_ Selection Changed() Query
function (that you will write in a coming step).Feature Layer MainWindow.xaml.csUse dark colors for code blocks Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line.
Add the parcels layer to the map and set selection properties
You now need to begin working in a different code file within this project.
First, add the LA parcels FeatureLayer
to the map's collection of data layers (GeoModel.OperationalLayers
). Providing the Layer.Id
allows for referencing the parcels layer from the layer collection when needed. Define the selection color via the map view's SelectionProperties
to distinguish selected features in the map.
In the Visual Studio > Solution Explorer, double-click MapViewModel.cs to open the file.
The project uses the Model-View-ViewModel (MVVM) design pattern to separate the application logic (view model) from the user interface (view).
Map
contains the view model class for the application, calledView Model.cs Map
. See the Microsoft documentation for more information about the Model-View-ViewModel pattern.View Model Add additional required
using
statements at the top of the class. These make your code more concise and allow you to use classes from these namespaces without having to fully qualify them.MapViewModel.csUse dark colors for code blocks Add line. Add line. Add line. Just after the section of code that defines the
Map
public property, create a similarSelection
public property as follows.Props MapViewModel.csUse dark colors for code blocks Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add code to the
Setup
function that adds the parcels feature layer to the map and sets the selection properties.Map() MapViewModel.csUse dark colors for code blocks Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line.
Create a function to query the parcels layer and select the result
In this step, create a new function that queries a FeatureLayer
(identified using its ID) using both attribute and spatial criteria. After clearing any currently selected features, a new query will be executed to find features in the map's current extent that meet the selected attribute expression. The features in the FeatureQueryResult
will be selected in the parcels layer.
Add the following new method named
Query
just after theFeature Layer() Setup
method.Map() MapViewModel.csUse dark colors for code blocks Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Click Debug > Start Debugging (or press <F5> on the keyboard) to run the app.
The app loads with the map centered on the Santa Monica Mountains in California with the parcels feature layer displayed. Choose an attribute expression, and parcels in the current extent that meet the selected criteria will display in the specified selection color.
What's next?
Learn how to use additional API features, ArcGIS location services, and ArcGIS tools in these tutorials: