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

An array of integer ObjectID values of the features in the feature service to Query for.

Syntax

Visual Basic (Declaration) 
Public Property ObjectIDs As Integer()
C# 
public int[] ObjectIDs {get; set;}

Remarks

ObjectID values are internal unique integers assigned to features in a feature service. ObjectID values are generated in the feature dataset when originally authored in ArcMap. Developers add the desired interger ObjectID values in an array and assign it to the Query.ObjectIDs Property to restrict which features are returned from the feature service. If an ObjectID is specified in the array but does not exist in the feature service, then that ObjectID value is ignored and not returned in the FeatureSet. No error is generated from non-existent integer ObjectID values passed to the Query.ObjectIDs Property.

One easy way a developer can discover what the ObjectID values are for the various features in feature dataset is to use the Query page of the ArcGIS Service Directory. In the following screen shot the Where clause is set to "1=1" (which returns all features) and the Return Fields is set to "STATE_NAME" (which is the 'Display Field'). Upon clicking either Query button, all of the features will be listed using the 'Display Field' and the ObjectID. The url that corresponse to this screen shot is:
http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5/query

Discovering the ObjectID values in a feature service via the ArcGIS Service Directory.

Which returns the results of:

Return results a Query via the ArcGIS Service Directory.

The ObjectIDs Property is one of the minimum Properties that must be set in order to have a valid Query object for use in a QueryTask operation. 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!

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 graphics in the map and limited 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.

Demonstrating using the Query.ObjectIDs Property in various ways.

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 3 states by ObjectIDs, version1 using Integer Array" IsChecked="True"/>
  <RadioButton Height="16" HorizontalAlignment="Left" Margin="12,173,0,0" Name="RadioButton2" VerticalAlignment="Top" 
  Content="Ex2: Get 3 states by ObjectIDs, version2 using Integer Array"/>
  <RadioButton Height="16" HorizontalAlignment="Left" Margin="12,195,0,0" Name="RadioButton3" VerticalAlignment="Top" 
  Content="Ex3: Get 3 states by ObjectIDs, version3 using Integer Array"/>
  <RadioButton Height="16" HorizontalAlignment="Left" Margin="12,217,0,0" Name="RadioButton4" VerticalAlignment="Top" 
  Content="Ex4: Get 2 states by ObjectIDs, one ObjectID is not valid; 1 feature actually returned"/>
  <RadioButton Height="16" HorizontalAlignment="Left" Margin="12,239,0,0" Name="RadioButton5" VerticalAlignment="Top" 
  Content="Ex5: Get 2 states by ObjectIDs AND all states via .Where; 2 features actually returned"/>
  <RadioButton Height="16" HorizontalAlignment="Left" Margin="12,261,0,0" Name="RadioButton6" VerticalAlignment="Top" 
  Content="Ex6: Get 2 states by ObjectIDs AND lower 48 states via .Geometry; 2 features actually returned"/>
  <RadioButton Height="16" HorizontalAlignment="Left" Margin="12,283,0,0" Name="RadioButton7" VerticalAlignment="Top" 
  Content="Ex7: Get 2 states by ObjectIDs AND 4 states by .Text; 2 features actually returned" />
  
  <!-- 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 graphics in the map and limited 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 = new ESRI.ArcGIS.Client.Tasks.QueryTask();
  myQueryTask.Url = "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)
  {
    // Defining Arrays Version 1
    
    // This option will return only those States in the FeatureLayer that have the ObjectIDs that are 
    // specified in the integer 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.
    
    // Specifying the ObjectID values directly in the Query.ObjectIDs Property.
    myQuery.ObjectIDs = new int[] {1, 3, 29};
    
    myQuery.ReturnGeometry = true;
    myQuery.OutSpatialReference = Map1.SpatialReference;
    myDisplayColor = Colors.Blue;
    }
  //--------------------------------------------------------------------------------------------------------
  if ((bool)RadioButton2.IsChecked)
  {
    // Defining Arrays Version 2
    
    // This option will return only those States in the FeatureLayer that have the ObjectIDs that are 
    // specified in the integer array (.ObjectIDs = {2, 10, 25}). 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.
    
    // Specifying the ObjectID values in an Integer Array. Then setting the Query.ObjectIDs to the Integer Array. 
    int[] myIntegerArray = {2, 10, 25};
    myQuery.ObjectIDs = myIntegerArray;
    
    myQuery.ReturnGeometry = true;
    myQuery.OutSpatialReference = Map1.SpatialReference;
    myDisplayColor = Colors.Brown;
  }
  //--------------------------------------------------------------------------------------------------------
  if ((bool)RadioButton3.IsChecked)
  {
    // Defining Arrays Version 3
    
    // This option will return only those States in the FeatureLayer that have the ObjectIDs that are 
    // specified in the integer array (.ObjectIDs = {5, 17, 33}). 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.
    
    // Creating an Integer Array with ability to hold three elements. Adding each ObjectID to the Integer
    // Array using its index poistion.
    int[] myIntegerArray = new int[3];
    myIntegerArray[0] = 5;
    myIntegerArray[1] = 17;
    myIntegerArray[2] = 33;
    myQuery.ObjectIDs = myIntegerArray;
    
    myQuery.ReturnGeometry = true;
    myQuery.OutSpatialReference = Map1.SpatialReference;
    myDisplayColor = Colors.Cyan;
  }
  //--------------------------------------------------------------------------------------------------------
  if ((bool)RadioButton4.IsChecked)
  {
    // This option will return only those States in the FeatureLayer that have the ObjectIDs that are 
    // specified in the integer array (.ObjectIDs = {11, 101}). One caveat is that the second value in the 
    // array (101) is not a valid ObjectID and will thus not be selected. 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.
    
    // Specifying the ObjectID values directly in the Query.ObjectIDs Property. One ObjectID is a good value (11),
    // the other ObjectID does not exists (101). The non-existent ObjectID just never shows; no error results.
    myQuery.ObjectIDs = new int[] {11, 101};
    
    myQuery.ReturnGeometry = true;
    myQuery.OutSpatialReference = Map1.SpatialReference;
    myDisplayColor = Colors.DarkGray;
  }
  //--------------------------------------------------------------------------------------------------------
  if ((bool)RadioButton5.IsChecked)
  {
    // This option will return only those States in the FeatureLayer that have the ObjectIDs that are 
    // specified in the integer array (.ObjectIDs = {15, 17}) AND that meet the conditions of the where
    // clause (.Where = "1=1"). The where clause in this example returns all the features but the ObjectIDs
    // only returns 2 features. Since both conditions must be true to return features; only two features 
    // will be 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 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"; // Gets all features
    myQuery.ObjectIDs = new int[] {15, 17}; // Gets 2 features
    
    myQuery.ReturnGeometry = true;
    myQuery.OutSpatialReference = Map1.SpatialReference;
    myDisplayColor = Colors.Green;
  }
  //--------------------------------------------------------------------------------------------------------
  if ((bool)RadioButton6.IsChecked)
  {
    // This option will return only those States in the FeatureLayer that have the ObjectIDs that are 
    // specified in the integer array (.ObjectIDs = {20, 35}) AND that meet the conditions of the features 
    // currently being visible in the Map's spatial extent. If you keep the default Map.Extent when running
    // the example code without zooming in/out, all states in the lower 48 will be selected. Since both 
    // conditions must be true to return features; only two features will be 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 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.Geometry = Map1.Extent; // Using default extent of the apps opening, the lower 48 states are selected
    myQuery.ObjectIDs = new int[] {20, 35}; // Gets 2 features
    
    myQuery.ReturnGeometry = true;
    myQuery.OutSpatialReference = Map1.SpatialReference;
    myDisplayColor = Colors.Yellow;
  }
  //--------------------------------------------------------------------------------------------------------
  if ((bool)RadioButton7.IsChecked)
  {
    // This option will return only those States in the FeatureLayer that have the ObjectIDs that are 
    // specified in the integer array (.ObjectIDs = {16, 41}) AND that meet the conditions of the features 
    // having the text "New" in the 'Display Field' called STATE_NAME. The Query.Text will return four 
    // features (New Hampshire, New York, New Jersey, New Mexico) but the Query.ObjectIDs only returns
    // two features (New York, New Mexico). Since both conditions must be true to return features; only 
    // two features will be 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 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.Text = "New"; // Gets 4 features
    myQuery.ObjectIDs = new int[] {16, 41}; //Gets 2 features
    
    myQuery.ReturnGeometry = true;
    myQuery.OutSpatialReference = Map1.SpatialReference;
    myDisplayColor = Colors.Magenta;
  }
  //--------------------------------------------------------------------------------------------------------
  
  // 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.
        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);
      }
    }
  }
  
  // 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 = New ESRI.ArcGIS.Client.Tasks.QueryTask
  myQueryTask.Url = "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
    
    ' Defining Arrays Version 1
    
    ' 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.
    
    ' Specifying the ObjectID values directly in the Query.ObjectIDs Property.
    myQuery.ObjectIDs = {1, 3, 29}
    
    myQuery.ReturnGeometry = True
    myQuery.OutSpatialReference = Map1.SpatialReference
    myDisplayColor = Colors.Blue
    
  End If
  '--------------------------------------------------------------------------------------------------------
  If RadioButton2.IsChecked Then
    
    ' Defining Arrays Version 2
    
    ' This option will return only those States in the FeatureLayer that have the ObjectIDs that are 
    ' specified in the interger array (.ObjectIDs = {2, 10, 25}). 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.
    
    ' Specifying the ObjectID values in an Integer Array. Then setting the Query.ObjectIDs to the Integer Array. 
    Dim myIntegerArray() As Integer = {2, 10, 25}
    myQuery.ObjectIDs = myIntegerArray
    
    myQuery.ReturnGeometry = True
    myQuery.OutSpatialReference = Map1.SpatialReference
    myDisplayColor = Colors.Brown
    
  End If
  '--------------------------------------------------------------------------------------------------------
  If RadioButton3.IsChecked Then
    
    ' Defining Arrays Version 3
    
    ' This option will return only those States in the FeatureLayer that have the ObjectIDs that are 
    ' specified in the interger array (.ObjectIDs = {5, 17, 33}). 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.
    
    ' Creating an Integer Array with ability to hold three elements. Adding each ObjectID to the Integer
    ' Array using its index poistion.
    Dim myIntegerArray(2) As Integer
    myIntegerArray(0) = 5
    myIntegerArray(1) = 17
    myIntegerArray(2) = 33
    myQuery.ObjectIDs = myIntegerArray
    
    myQuery.ReturnGeometry = True
    myQuery.OutSpatialReference = Map1.SpatialReference
    myDisplayColor = Colors.Cyan
    
  End If
  '--------------------------------------------------------------------------------------------------------
  If RadioButton4.IsChecked Then
    
    ' This option will return only those States in the FeatureLayer that have the ObjectIDs that are 
    ' specified in the interger array (.ObjectIDs = {11, 101}). One caveat is that the second value in the 
    ' array (101) is not a valid ObjectID and will thus not be selected. 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.
    
    ' Specifying the ObjectID values directly in the Query.ObjectIDs Property. One ObjectID is a good value (11),
    ' the other ObjectID does not exists (101). The non-existent ObjectID just never shows; no error results.
    myQuery.ObjectIDs = {11, 101}
    
    myQuery.ReturnGeometry = True
    myQuery.OutSpatialReference = Map1.SpatialReference
    myDisplayColor = Colors.DarkGray
    
  End If
  '--------------------------------------------------------------------------------------------------------
  If RadioButton5.IsChecked Then
    
    ' This option will return only those States in the FeatureLayer that have the ObjectIDs that are 
    ' specified in the interger array (.ObjectIDs = {15, 17}) AND that meet the conditions of the where
    ' clause (.Where = "1=1"). The where clause in this example returns all the features but the ObjectIDs
    ' only returns 2 features. Since both conditions must be true to return features; only two features 
    ' will be 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 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" ' Gets all features
    myQuery.ObjectIDs = {15, 17} ' Gets 2 features
    
    myQuery.ReturnGeometry = True
    myQuery.OutSpatialReference = Map1.SpatialReference
    myDisplayColor = Colors.Green
    
  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 = {20, 35}) AND that meet the conditions of the features 
    ' currently being visible in the Map's spatial extent. If you keep the default Map.Extent when running
    ' the example code without zooming in/out, all states in the lower 48 will be selected. Since both 
    ' conditions must be true to return features; only two features will be 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 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.Geometry = Map1.Extent ' Using default extent of the apps opening, the lower 48 states are selected
    myQuery.ObjectIDs = {20, 35} ' Gets 2 features
    
    myQuery.ReturnGeometry = True
    myQuery.OutSpatialReference = Map1.SpatialReference
    myDisplayColor = Colors.Yellow
    
  End If
  '--------------------------------------------------------------------------------------------------------
  If RadioButton7.IsChecked Then
    
    ' This option will return only those States in the FeatureLayer that have the ObjectIDs that are 
    ' specified in the interger array (.ObjectIDs = {16, 41}) AND that meet the conditions of the features 
    ' having the text "New" in the 'Display Field' called STATE_NAME. The Query.Text will return four 
    ' features (New Hampshire, New York, New Jersey, New Mexico) but the Query.ObjectIDs only returns
    ' two features (New York, New Mexico). Since both conditions must be true to return features; only 
    ' two features will be 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 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.Text = "New" ' Gets 4 features
    myQuery.ObjectIDs = {16, 41} 'Gets 2 features
    
    myQuery.ReturnGeometry = True
    myQuery.OutSpatialReference = Map1.SpatialReference
    myDisplayColor = Colors.Magenta
    
  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.
        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 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
            
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

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.