ArcGIS API for Silverlight - Library Reference
Query Class
Members  Example  See Also  Send comments on this topic
ESRI.ArcGIS.Client.Tasks Namespace : Query Class

The parameters used to execute the QueryTask.

Object Model

Query ClassGeometry ClassOutFields ClassString ClassSpatialReference ClassLayerSource ClassTimeExtent ClassTimeOption Class

Syntax

Visual Basic (Declaration) 
Public Class Query 
C# 
public class Query 

Remarks

The Query Class provides numerous Properties that can be used in conjunction with one another to form queries that obtain data from ArcGIS Server feature services via the QueryTask.

Typically, the Query object is passed in as a parameter to the QueryTask.ExecuteAsync(Query) or QueryTask.ExecuteAsync(Query,Object) Methods. These Methods invoke an asynchronous execution on ArcGIS Server to generate a FeatureSet that is returned to the client application. The return results come back in the QueryTask.ExecuteCompleted Event and are accessed via the QueryEventArgs.

Another option for using the Query object is as a parameter of the QueryTask.ExecuteCountAsync Method. This is also an asynchronous execution that returns results in the QueryTask.ExecuteCountCompleted Event and are accessed via the QueryCountEventArgs.

The number-of and combination-of Properties that need to be set in the Query object order to obtain valid results from a QueryTask's execution vary widely. The queries can be spatial, textual, or a combination of two. At a minimum at least one of the following Query Properties must be used in order to obtain valid results from a QueryTask:

If none of the above listed Query Properties are used, then an error message similar to the following will be returned in the QueryTask.Failed Event:

'Error code '400': 'Unable to complete operation.'
'where' parameter not specified
'objectIds' parameter not specified
'time' parameter not specified
'geometry' parameter not specified
'text' parameter not specified
'Unable to complete Query operation.

The other Properties on the Query class may also be necessary to augment the spatial or textual query to obtain the results desired. When setting multiple spatial or textual Query Property values (those listed in the bulleted list above) they act and as an 'AND' type of condition for retrieving results from the feature service. This means that only those features that meet the conditions of both these spatial/textual queries will be returned in the FeatureSet. The number of combinations that can be used to filter which data is returned is nearly limitless.

NOTE: ** Query.Text and Query.Where are the only exceptions to this rule of setting both of these Query Properties acting as an 'AND'. When these two properties are both set, the Query.Where Property take precedence and the Query.Text Property is ignored!

Both MapServer and FeatureServer types of ArcGIS Server features services can have the ability to perform QueryTask operations if they are enabled. To determine which type of ArcGIS Server feature services have the ability to perform QueryTask operations, use the ArcGIS Services Directory. Copy the Url of the desired ArcGIS Server into the address bar of a web browser:

Using the ArcGIS Service Directory Diagram 1.

Then click on the desired MapServer or FeatureServer hyperlink (you may need to traverse some folders if the feature service is nested in a hierarchy):

Using the ArcGIS Service Directory Diagram 2.

Then click on a sub-layer; it will have its integer ID value in parenthesis:

Using the ArcGIS Service Directory Diagram 3.

You will know if the feature service supports a QueryTask if you scroll down towards the bottom of the page and find the word "Query" in the Supported Operations section. If the word "Query" is not in the Supported Operations section for the specific sub-layer of the MapServer> or FeatureServer then you will not be able to perform a QueryTask on it:

Using the ArcGIS Service Directory Diagram 4.

Example

How to use:

Select one of the various RadioButtons and then click the 'Run QueryTask' button. The result of the QueryTask operation will display (depending on the task) graphics in the map and/or attribute information in the TextBox. Read the comments in the code-behind for more details on exactly what is occurring in defining the Query Properties for the QueryTask.

The XAML code in this example is used in conjunction with the code-behind (C# or VB.NET) to demonstrate the functionality.

The following screen shot corresponds to the code example in this page.

Testing how the various Query Properties can be adjusted.

XAMLCopy Code
<Grid x:Name="LayoutRoot">
  
  <!-- Add a Map Control. Zoom to the continental US. -->
  <esri:Map Background="White" Name="Map1" WrapAround="True" Margin="7,311,0,0" HorizontalAlignment="Left" 
        VerticalAlignment="Top" Height="282" Width="370" Extent="-14187754,2073238,-7404583,7243114">
  
    <!-- Add a backdrop ArcGISTiledMapServiceLayer. -->
    <esri:ArcGISTiledMapServiceLayer ID="PhysicalTiledLayer"
          Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
    
  </esri:Map>
  
  <!-- 
  Add controls for the user to interact with and see the effect of setting different properties of the Query 
  object that is used in a QueryTask. 
  -->
  <TextBox Height="282" HorizontalAlignment="Left" Margin="383,311,0,0" Name="TextBox1" 
           VerticalAlignment="Top" Width="206" VerticalScrollBarVisibility="Visible" />
  <Button Content="Run QueryTask" Height="32" HorizontalAlignment="Left" Margin="471,144,0,0" 
          Name="Button_RunQueryTask" VerticalAlignment="Top" Width="114" Click="Button_RunQueryTask_Click"/>
  <RadioButton Height="16" HorizontalAlignment="Left" Margin="12,151,0,0" Name="RadioButton1" VerticalAlignment="Top" 
               Content="Ex1: Get all states; NO graphics, attributes limited" IsChecked="True"/>
  <RadioButton Height="16" HorizontalAlignment="Left" Margin="12,173,0,0" Name="RadioButton2" VerticalAlignment="Top" 
               Content="Ex2: Get all states; display graphics, display attributes"/>
  <RadioButton Height="16" HorizontalAlignment="Left" Margin="12,195,0,0" Name="RadioButton3" VerticalAlignment="Top" 
               Content="Ex3: Get states in the current map extent; display graphics, display attributes"/>
  <RadioButton Height="16" HorizontalAlignment="Left" Margin="12,217,0,0" Name="RadioButton4" VerticalAlignment="Top" 
               Content="Ex4: Get states in current map extent AND with pop > 10,000,000; display graphics, diaplay attributes"/>
  <RadioButton Height="16" HorizontalAlignment="Left" Margin="12,239,0,0" Name="RadioButton5" VerticalAlignment="Top" 
               Content="Ex5: Get states with the words 'North' in DisplayField; NO graphics, display selected field attributes"/>
  <RadioButton Height="16" HorizontalAlignment="Left" Margin="12,261,0,0" Name="RadioButton6" VerticalAlignment="Top" 
               Content="Ex6: Get a few states by ObjectID; display graphics, attributes limited"/>
  <RadioButton Height="16" HorizontalAlignment="Left" Margin="12,283,0,0" Name="RadioButton7" VerticalAlignment="Top" 
               Content="Ex7: Get all states; display GENERALIZED graphics, attributes limited" />
  
  <!-- Provide the instructions on how to use the sample code. -->
  <TextBlock Height="138" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" Width="590" 
  TextWrapping="Wrap" Text="Select one of the various RadioButtons and then click the 'Run QueryTask' button. 
  The result of the QueryTask operation will display (depending on the task) graphics in the map and/or 
  attribute information in the TextBox. Read the comments in the code-behind for more details on exactly
  what is occurring in defining the Query Properties for the QueryTask."/>
  
</Grid>
C#Copy Code
private void Button_RunQueryTask_Click(object sender, System.Windows.RoutedEventArgs e)
{
  // This function will execute when the user clicks the 'Run QueryTask' button. The purpose is to
  // demonstrate various ways that a Query can be constructed as an input for the QueryTask operation.
  // The number of permutations of setting the various properties on the Query object is nearly 
  // limitless. 
  
  // Create a new color object. A different color will be used for each RadioButton that displays
  // back graphical features as a result of a single QueryTask run. The color object will be 
  // passed as the UserToken parameter for the QueryTask.
  System.Windows.Media.Color myDisplayColor = new System.Windows.Media.Color();
  
  // Create a new QueryTask object. Set the Url to be a sample public FeatureLayer.
  ESRI.ArcGIS.Client.Tasks.QueryTask myQueryTask = null;
  myQueryTask = new ESRI.ArcGIS.Client.Tasks.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5");
  
  // Wire up the event handler for when the QueryTask completes.
  myQueryTask.ExecuteCompleted += myQueryTask_ExecuteCompleted;
  
  // Wire up the event handler for when the QueryTask in case there is a problem.
  myQueryTask.Failed += myQueryTask_Failed;
  
  // Create a new Query object. Depending on the RadioButton chosen by the user, several different
  // Properties will be set that return different results.
  ESRI.ArcGIS.Client.Tasks.Query myQuery = new ESRI.ArcGIS.Client.Tasks.Query();
  
  //--------------------------------------------------------------------------------------------------------
  if ((bool)RadioButton1.IsChecked)
  {
    // This option will return all of the States in the FeatureLayer. No graphic features will be returned and
    // thus not displayed in the Map. Also no attributes were specified as being returned but since the
    // ArcGIS Server feature service has the 'Display Field' of STATE_NAME specified, it will return the names
    // of each state.
    
    myQuery.Where = "1=1";
  }
  //--------------------------------------------------------------------------------------------------------
  if ((bool)RadioButton2.IsChecked)
  {
    // This option will return all of the States in the FeatureLayer (.Where = "1=1"). The graphic features will 
    // be returned (.ReturnGeometry = True) and displayed in the Map (using the same geographic space as the Map, 
    // (.OutSpatialReference = Map1.Extent). Also all attributes were specified as being returned 
    // (.OutFields.Add("*")) and displayed in the TextBox.
    
    myQuery.Where = "1=1";
    myQuery.OutFields.Add("*");
    myQuery.ReturnGeometry = true;
    myQuery.OutSpatialReference = Map1.SpatialReference;
    myDisplayColor = Colors.Blue;
  }
  //--------------------------------------------------------------------------------------------------------
  if ((bool)RadioButton3.IsChecked)
  {
    // This option will return all of the States in the FeatureLayer that are contained within or intersect the 
    // Map current Extent (.Geometry = Map1.Extent). Experiment by zooming in and clicking the button and then 
    // zooming back out to see that only the features in the prior extent were returned. 
    // The graphic features will be returned (.ReturnGeometry = True) and displayed in the Map (using the same
    // geographic space as the Map, (.OutSpatialReference = Map1.Extent). Also all attributes were specified as 
    // being returned (.OutFields.Add("*")) and displayed in the TextBox.
    
    myQuery.Geometry = Map1.Extent;
    myQuery.OutFields.Add("*");
    myQuery.ReturnGeometry = true;
    myQuery.OutSpatialReference = Map1.SpatialReference;
    myDisplayColor = Colors.Yellow;
  }
  //--------------------------------------------------------------------------------------------------------
  if ((bool)RadioButton4.IsChecked)
  {
    // This option will return all of the States in the FeatureLayer that are contained within or intersect the 
    // Map current Extent (.Geometry = Map1.Extent) AND that have a population of greater that 10 million 
    // people (.Where = "POP2000 > 10000000". 
    // The graphic features will be returned (.ReturnGeometry = True) and displayed in the Map (using the same
    // geographic space as the Map, (.OutSpatialReference = Map1.Extent). Also all attributes were specified as 
    // being returned (.OutFields.Add("*")) and displayed in the TextBox.
    
    myQuery.Geometry = Map1.Extent;
    myQuery.Where = "POP2000 > 10000000";
    myQuery.OutFields.Add("*");
    myQuery.ReturnGeometry = true;
    myQuery.OutSpatialReference = Map1.SpatialReference;
    myDisplayColor = Colors.Red;
  }
  //--------------------------------------------------------------------------------------------------------
  if ((bool)RadioButton5.IsChecked)
  {
    // This option will return only those States in the FeatureLayer that have the word "North" in the 
    // 'Display Field' defined by the feature service. The 'Display Field' is POP2000. No graphic features 
    // will be returned (.ReturnGeometry = False) and hence will not be displayed in the Map. Also only two 
    // attributes are specified as being returned (.OutFields.Add("STATE_NAME") and .OutFields.Add("POP2000") 
    // displayed in the TextBox.
    
    myQuery.Text = "North";
    myQuery.OutFields.Add("STATE_NAME");
    myQuery.OutFields.Add("POP2000");
    myQuery.ReturnGeometry = false;
  }
  //--------------------------------------------------------------------------------------------------------
  if ((bool)RadioButton6.IsChecked)
  {
    // This option will return only those States in the FeatureLayer that have the ObjectIDs that are 
    // specified in the interger array (.ObjectIDs = {1, 3, 29}). The graphic features will be returned 
    // (.ReturnGeometry = True) and displayed in the Map (using the same geographic space as the Map, 
    // (.OutSpatialReference = Map1.Extent). Also no attributes were specified as being returned but since the
    // ArcGIS Server feature service has the 'Display Field' of STATE_NAME specified, it will return the names
    // of each state.
    
    myQuery.ObjectIDs = new int[]{1, 3, 29};
    myQuery.ReturnGeometry = true;
    myQuery.OutSpatialReference = Map1.SpatialReference;
    myDisplayColor = Colors.Purple;
  }
  
  //--------------------------------------------------------------------------------------------------------
  if ((bool)RadioButton7.IsChecked)
  {
    // This option will return all of the States in the FeatureLayer (.Where = "1=1"). The graphic features will 
    // be returned (.ReturnGeometry = True) and displayed in the Map (using the same geographic space as the Map, 
    // (.OutSpatialReference = Map1.Extent). The returned graphics from the QueryTask are generalized to have less
    // vertices in their features and will look different from the original FeatureLayer (.MaxAllowableOffset = 200000)
    // Also no attributes were specified as being returned but since the ArcGIS Server feature service has the 
    // 'Display Field' of STATE_NAME specified, it will return the names of each state.
    
    myQuery.Where = "1=1";
    myQuery.ReturnGeometry = true;
    myQuery.OutSpatialReference = Map1.SpatialReference;
    myQuery.MaxAllowableOffset = 200000; //Makes highly generalized output features!
    myDisplayColor = Colors.Orange;
  }
  //--------------------------------------------------------------------------------------------------------
  
  // Execute the QueryTask using the Query object options chosen by the user. Pass in the color as the userToken.
  myQueryTask.ExecuteAsync(myQuery, myDisplayColor);
}
            
private void myQueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs e)
{
  //Clear out any existing results in the TextBox and the Map.
  TextBox1.Text = "";
  if (Map1.Layers.Count > 1)
  {
    Map1.Layers.RemoveAt(1);
  }
  
  // Get the returned FeatureSet from the QueryTask.
  ESRI.ArcGIS.Client.Tasks.FeatureSet myFeatureSet = e.FeatureSet;
  
  // Set the display color of the Graphics to that of the UserState object passed into the QueryTask.
  System.Windows.Media.Color myDisplayColor = (System.Windows.Media.Color)e.UserState;
  
  // Construct a StringBuilder object to hold all of the TextBox information to display to the user.
  System.Text.StringBuilder myStringBuilderDisplayText = new System.Text.StringBuilder();
  
  // Get the count of the number of features returned and add to the StringBuilder object.
  int theCountOfFeatures = myFeatureSet.Count();
  myStringBuilderDisplayText.Append("FeatureSet Count: " + theCountOfFeatures.ToString() + Environment.NewLine);
  myStringBuilderDisplayText.Append("=======================================" + Environment.NewLine);
  
  // Create a new GraphicsLayer and set its ID.
  ESRI.ArcGIS.Client.GraphicsLayer myGraphicsLayer = new ESRI.ArcGIS.Client.GraphicsLayer();
  myGraphicsLayer.ID = "TheGraphicsLayer";
  
  // Create a SimpleFillSymbol to draw the graphics features added to the GraphicsLayer. Set the
  // color of the solid fill to be what was passed via the userToken.
  ESRI.ArcGIS.Client.Symbols.SimpleFillSymbol mySimpleFillSymbol = new ESRI.ArcGIS.Client.Symbols.SimpleFillSymbol();
  System.Windows.Media.SolidColorBrush mySolidColorBrush = new System.Windows.Media.SolidColorBrush(myDisplayColor);
  mySolidColorBrush.Opacity = 0.5;
  mySimpleFillSymbol.Fill = mySolidColorBrush;
  
  // Ensure the we have a FeatureSet with data.
  if (myFeatureSet != null && myFeatureSet.Features.Count > 0)
  {
    // Iterate over each feature (which is a Graphic object) in the FeatureSet.
    foreach (ESRI.ArcGIS.Client.Graphic graphicFeature in myFeatureSet.Features)
    {
      if (graphicFeature.Geometry is ESRI.ArcGIS.Client.Geometry.Polygon)
      {
        // The Graphic returned has geometry values. 
        
        // Attach the custom solid fill symbol to the Graphic.
        graphicFeature.Symbol = mySimpleFillSymbol as ESRI.ArcGIS.Client.Symbols.Symbol;
        
        // Add the Graphic to the GraphicsLayer
        myGraphicsLayer.Graphics.Add(graphicFeature);
        
        // Add the attribute information associated with the Graphic to the StringBuilder object.
        DisplayAttributeinformation(graphicFeature, myStringBuilderDisplayText);
      }
      else //If TypeOf graphicFeature.Geometry Is ESRI.ArcGIS.Client.Geometry.Polygon Then
      {
        // The Graphic returned has DOES NOT have geometry values. 
        
        // No Geometry was returned. Thus there are no features to display in the map.
        myStringBuilderDisplayText.Append("No Geometry returned." + Environment.NewLine);
        
        // There still may be some attribute information associated with the Graphic so add it
        // to the StringBuilder object.
        DisplayAttributeinformation(graphicFeature, myStringBuilderDisplayText);
      }
    }
  }
  
  // Add the GraphicsLayer to the Map. It will automatically draw; there is not a need for a refresh.
  Map1.Layers.Add(myGraphicsLayer);
  
  // Display the count of the features returned as well as any attribute information.
  TextBox1.Text = myStringBuilderDisplayText.ToString();
}
            
public void DisplayAttributeinformation(ESRI.ArcGIS.Client.Graphic graphicFeature, System.Text.StringBuilder myStringBuilderDisplayText)
{
  // Subroutine to loop though all of the attributes associated with a Graphic and add to a StringBuilder object.
  System.Collections.Generic.IDictionary<string, object> allAttributes = graphicFeature.Attributes;
  foreach (string oneKey in allAttributes.Keys)
  {
    object theValue = allAttributes[oneKey];
    myStringBuilderDisplayText.Append(oneKey + ": " + theValue.ToString() + Environment.NewLine);
  }
  // Add line break between attributes.
  myStringBuilderDisplayText.Append(Environment.NewLine);
}
            
private void myQueryTask_Failed(object sender, ESRI.ArcGIS.Client.Tasks.TaskFailedEventArgs e)
{
  // Clear out any existing results.
  TextBox1.Text = "";
  
  // Display any error message information about the QueryTask to the user.
  TextBox1.Text = "ERROR: " + Environment.NewLine + e.Error.ToString();
}
VB.NETCopy Code
Private Sub Button_RunQueryTask_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
  
  ' This function will execute when the user clicks the 'Run QueryTask' button. The purpose is to
  ' demonstrate various ways that a Query can be constructed as an input for the QueryTask operation.
  ' The number of permutations of setting the various properties on the Query object is nearly 
  ' limitless. 
  
  ' Create a new color object. A different color will be used for each RadioButton that displays
  ' back graphical features as a result of a single QueryTask run. The color object will be 
  ' passed as the UserToken parameter for the QueryTask.
  Dim myDisplayColor As System.Windows.Media.Color = New System.Windows.Media.Color
  
  ' Create a new QueryTask object. Set the Url to be a sample public FeatureLayer.
  Dim myQueryTask As ESRI.ArcGIS.Client.Tasks.QueryTask = Nothing
  myQueryTask = New ESRI.ArcGIS.Client.Tasks.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5")
  
  ' Wire up the event handler for when the QueryTask completes.
  AddHandler myQueryTask.ExecuteCompleted, AddressOf myQueryTask_ExecuteCompleted
  
  ' Wire up the event handler for when the QueryTask in case there is a problem.
  AddHandler myQueryTask.Failed, AddressOf myQueryTask_Failed
  
  ' Create a new Query object. Depending on the RadioButton chosen by the user, several different
  ' Properties will be set that return different results.
  Dim myQuery As ESRI.ArcGIS.Client.Tasks.Query = New ESRI.ArcGIS.Client.Tasks.Query
  
  '--------------------------------------------------------------------------------------------------------
  If RadioButton1.IsChecked Then
    
    ' This option will return all of the States in the FeatureLayer. No graphic features will be returned and
    ' thus not displayed in the Map. Also no attributes were specified as being returned but since the
    ' ArcGIS Server feature service has the 'Display Field' of STATE_NAME specified, it will return the names
    ' of each state.
    
    myQuery.Where = "1=1"
    
  End If
  '--------------------------------------------------------------------------------------------------------
  If RadioButton2.IsChecked Then
    
    ' This option will return all of the States in the FeatureLayer (.Where = "1=1"). The graphic features will 
    ' be returned (.ReturnGeometry = True) and displayed in the Map (using the same geographic space as the Map, 
    ' (.OutSpatialReference = Map1.Extent). Also all attributes were specified as being returned 
    ' (.OutFields.Add("*")) and displayed in the TextBox.
    
    myQuery.Where = "1=1"
    myQuery.OutFields.Add("*")
    myQuery.ReturnGeometry = True
    myQuery.OutSpatialReference = Map1.SpatialReference
    myDisplayColor = Colors.Blue
    
  End If
  '--------------------------------------------------------------------------------------------------------
  If RadioButton3.IsChecked Then
    
    ' This option will return all of the States in the FeatureLayer that are contained within or intersect the 
    ' Map current Extent (.Geometry = Map1.Extent). Experiment by zooming in and clicking the button and then 
    ' zooming back out to see that only the features in the prior extent were returned. 
    ' The graphic features will be returned (.ReturnGeometry = True) and displayed in the Map (using the same
    ' geographic space as the Map, (.OutSpatialReference = Map1.Extent). Also all attributes were specified as 
    ' being returned (.OutFields.Add("*")) and displayed in the TextBox.
    
    myQuery.Geometry = Map1.Extent
    myQuery.OutFields.Add("*")
    myQuery.ReturnGeometry = True
    myQuery.OutSpatialReference = Map1.SpatialReference
    myDisplayColor = Colors.Yellow
    
  End If
  '--------------------------------------------------------------------------------------------------------
  If RadioButton4.IsChecked Then
    
    ' This option will return all of the States in the FeatureLayer that are contained within or intersect the 
    ' Map current Extent (.Geometry = Map1.Extent) AND that have a population of greater that 10 million 
    ' people (.Where = "POP2000 > 10000000". 
    ' The graphic features will be returned (.ReturnGeometry = True) and displayed in the Map (using the same
    ' geographic space as the Map, (.OutSpatialReference = Map1.Extent). Also all attributes were specified as 
    ' being returned (.OutFields.Add("*")) and displayed in the TextBox.
    
    myQuery.Geometry = Map1.Extent
    myQuery.Where = "POP2000 > 10000000"
    myQuery.OutFields.Add("*")
    myQuery.ReturnGeometry = True
    myQuery.OutSpatialReference = Map1.SpatialReference
    myDisplayColor = Colors.Red
    
  End If
  '--------------------------------------------------------------------------------------------------------
  If RadioButton5.IsChecked Then
    
    ' This option will return only those States in the FeatureLayer that have the word "North" in the 
    ' 'Display Field' defined by the feature service. The 'Display Field' is POP2000. No graphic features 
    ' will be returned (.ReturnGeometry = False) and hence will not be displayed in the Map. Also only two 
    ' attributes are specified as being returned (.OutFields.Add("STATE_NAME") and .OutFields.Add("POP2000") 
    ' displayed in the TextBox.
    
    myQuery.Text = "North"
    myQuery.OutFields.Add("STATE_NAME")
    myQuery.OutFields.Add("POP2000")
    myQuery.ReturnGeometry = False
    
  End If
  '--------------------------------------------------------------------------------------------------------
  If RadioButton6.IsChecked Then
    
    ' This option will return only those States in the FeatureLayer that have the ObjectIDs that are 
    ' specified in the interger array (.ObjectIDs = {1, 3, 29}). The graphic features will be returned 
    ' (.ReturnGeometry = True) and displayed in the Map (using the same geographic space as the Map, 
    ' (.OutSpatialReference = Map1.Extent). Also no attributes were specified as being returned but since the
    ' ArcGIS Server feature service has the 'Display Field' of STATE_NAME specified, it will return the names
    ' of each state.
    
    myQuery.ObjectIDs = {1, 3, 29}
    myQuery.ReturnGeometry = True
    myQuery.OutSpatialReference = Map1.SpatialReference
    myDisplayColor = Colors.Purple
    
  End If
  '--------------------------------------------------------------------------------------------------------
  If RadioButton7.IsChecked Then
    
    ' This option will return all of the States in the FeatureLayer (.Where = "1=1"). The graphic features will 
    ' be returned (.ReturnGeometry = True) and displayed in the Map (using the same geographic space as the Map, 
    ' (.OutSpatialReference = Map1.Extent). The returned graphics from the QueryTask are generalized to have less
    ' vertices in their features and will look different from the original FeatureLayer (.MaxAllowableOffset = 200000)
    ' Also no attributes were specified as being returned but since the ArcGIS Server feature service has the 
    ' 'Display Field' of STATE_NAME specified, it will return the names of each state.
    
    myQuery.Where = "1=1"
    myQuery.ReturnGeometry = True
    myQuery.OutSpatialReference = Map1.SpatialReference
    myQuery.MaxAllowableOffset = 200000 'Makes highly generalized output features!
    myDisplayColor = Colors.Orange
    
  End If
  '--------------------------------------------------------------------------------------------------------
  
  ' Execute the QueryTask using the Query object options chosen by the user. Pass in the color as the userToken.
  myQueryTask.ExecuteAsync(myQuery, myDisplayColor)
  
End Sub
            
Private Sub myQueryTask_ExecuteCompleted(sender As Object, e As ESRI.ArcGIS.Client.Tasks.QueryEventArgs)
  
  'Clear out any existing results in the TextBox and the Map.
  TextBox1.Text = ""
  If Map1.Layers.Count > 1 Then
    Map1.Layers.RemoveAt(1)
  End If
  
  ' Get the returned FeatureSet from the QueryTask.
  Dim myFeatureSet As ESRI.ArcGIS.Client.Tasks.FeatureSet = e.FeatureSet
  
  ' Set the display color of the Graphics to that of the UserState object passed into the QueryTask.
  Dim myDisplayColor As System.Windows.Media.Color = e.UserState
  
  ' Construct a StringBuilder object to hold all of the TextBox information to display to the user.
  Dim myStringBuilderDisplayText As Text.StringBuilder = New Text.StringBuilder
  
  ' Get the count of the number of features returned and add to the StringBuilder object.
  Dim theCountOfFeatures As Integer = myFeatureSet.Count
  myStringBuilderDisplayText.Append("FeatureSet Count: " + theCountOfFeatures.ToString + vbCrLf)
  myStringBuilderDisplayText.Append("=======================================" + vbCrLf)
  
  ' Create a new GraphicsLayer and set its ID.
  Dim myGraphicsLayer As ESRI.ArcGIS.Client.GraphicsLayer = New ESRI.ArcGIS.Client.GraphicsLayer
  myGraphicsLayer.ID = "TheGraphicsLayer"
  
  ' Create a SimpleFillSymbol to draw the graphics features added to the GraphicsLayer. Set the
  ' color of the solid fill to be what was passed via the userToken.
  Dim mySimpleFillSymbol As New ESRI.ArcGIS.Client.Symbols.SimpleFillSymbol
  Dim mySolidColorBrush As New System.Windows.Media.SolidColorBrush(myDisplayColor)
  mySolidColorBrush.Opacity = 0.5
  mySimpleFillSymbol.Fill = mySolidColorBrush
  
  ' Ensure the we have a FeatureSet with data.
  If myFeatureSet IsNot Nothing AndAlso myFeatureSet.Features.Count > 0 Then
  
    ' Iterate over each feature (which is a Graphic object) in the FeatureSet.
    For Each graphicFeature As ESRI.ArcGIS.Client.Graphic In myFeatureSet.Features
      
      If TypeOf graphicFeature.Geometry Is ESRI.ArcGIS.Client.Geometry.Polygon Then
        
        ' The Graphic returned has geometry values. 
        
        ' Attach the custom solid fill symbol to the Graphic.
        graphicFeature.Symbol = TryCast(mySimpleFillSymbol, ESRI.ArcGIS.Client.Symbols.Symbol)
        
        ' Add the Graphic to the GraphicsLayer
        myGraphicsLayer.Graphics.Add(graphicFeature)
        
        ' Add the attribute information associated with the Graphic to the StringBuilder object.
        DisplayAttributeinformation(graphicFeature, myStringBuilderDisplayText)
        
      Else 'If TypeOf graphicFeature.Geometry Is ESRI.ArcGIS.Client.Geometry.Polygon Then
        
        ' The Graphic returned has DOES NOT have geometry values. 
        
        ' No Geometry was returned. Thus there are no features to display in the map.
        myStringBuilderDisplayText.Append("No Geometry returned." + vbCrLf)
        
        ' There still may be some attribute information associated with the Graphic so add it
        ' to the StringBuilder object.
        DisplayAttributeinformation(graphicFeature, myStringBuilderDisplayText)
        
      End If
      
    Next graphicFeature
    
  End If
  
  ' Add the GraphicsLayer to the Map. It will automatically draw; there is not a need for a refresh.
  Map1.Layers.Add(myGraphicsLayer)
  
  ' Display the count of the features returned as well as any attribute information.
  TextBox1.Text = myStringBuilderDisplayText.ToString
  
End Sub
            
Public Sub DisplayAttributeinformation(graphicFeature As ESRI.ArcGIS.Client.Graphic, myStringBuilderDisplayText As Text.StringBuilder)
  
  ' Subroutine to loop though all of the attributes associated with a Graphic and add to a StringBuilder object.
  Dim allAttributes As System.Collections.Generic.IDictionary(Of String, Object) = graphicFeature.Attributes
  For Each oneKey As String In allAttributes.Keys
    Dim theValue As Object = allAttributes.Item(oneKey)
    myStringBuilderDisplayText.Append(oneKey + ": " + theValue.ToString + vbCrLf)
  Next
  
  ' Add line break between attributes.
  myStringBuilderDisplayText.Append(vbCrLf)
  
End Sub
            
Private Sub myQueryTask_Failed(sender As Object, e As ESRI.ArcGIS.Client.Tasks.TaskFailedEventArgs)
  
  ' Clear out any existing results.
  TextBox1.Text = ""
  
  ' Display any error message information about the QueryTask to the user.
  TextBox1.Text = "ERROR: " + vbCrLf + e.Error.ToString
  
End Sub

Inheritance Hierarchy

System.Object
   ESRI.ArcGIS.Client.Tasks.Query

Requirements

Target Platforms: Windows XP Professional, Windows Server 2003 family, Windows Vista, Windows Server 2008 family, Windows 7

See Also

© ESRI, Inc. All Rights Reserved.