This document is archived and information here might be outdated.  Recommended version.


Select Map Features by Attribute Query Snippet (ArcObjects .NET 10.6 SDK)
ArcObjects Library Reference

Select Map Features by Attribute Query Snippet

Select features in the IActiveView by an attribute query using a SQL syntax in a where clause.

[C#]
///<summary>Select features in the IActiveView by an attribute query using a SQL syntax in a where clause.</summary>
/// 
///<param name="activeView">An IActiveView interface</param>
///<param name="featureLayer">An IFeatureLayer interface to select upon</param>
///<param name="whereClause">A System.String that is the SQL where clause syntax to select features. Example: "CityName='Redlands'"</param>
///  
///<remarks>Providing and empty string "" will return all records.</remarks>
public void SelectMapFeaturesByAttributeQuery(ESRI.ArcGIS.Carto.IActiveView activeView, ESRI.ArcGIS.Carto.IFeatureLayer featureLayer, System.String whereClause)
{
  if(activeView == null || featureLayer == null || whereClause == null)
  {
    return;
  }
  ESRI.ArcGIS.Carto.IFeatureSelection featureSelection=featureLayer as ESRI.ArcGIS.Carto.IFeatureSelection; // Dynamic Cast

  // Set up the query
  ESRI.ArcGIS.Geodatabase.IQueryFilter queryFilter=new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
  queryFilter.WhereClause=whereClause;

  // Invalidate only the selection cache. Flag the original selection
  activeView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGeoSelection, null, null);

  // Perform the selection
  featureSelection.SelectFeatures(queryFilter, ESRI.ArcGIS.Carto.esriSelectionResultEnum.esriSelectionResultNew, false);

  // Flag the new selection
  activeView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGeoSelection, null, null);
}
[Visual Basic .NET]
'''<summary>Select features in the IActiveView by an attribute query using a SQL syntax in a where clause.</summary>
''' 
'''<param name="activeView">An IActiveView interface</param>
'''<param name="featureLayer">An IFeatureLayer interface to select upon</param>
'''<param name="whereClause">A System.String that is the SQL where clause syntax to select features. Example: "CityName='Redlands'"</param>
'''  
'''<remarks>Providing and empty string "" will return all records.</remarks>
Public Sub SelectMapFeaturesByAttributeQuery(ByVal activeView As ESRI.ArcGIS.Carto.IActiveView, ByVal featureLayer As ESRI.ArcGIS.Carto.IFeatureLayer, ByVal whereClause As System.String)

  If activeView Is Nothing OrElse featureLayer Is Nothing OrElse whereClause Is Nothing Then
        Return
  End If

  Dim featureSelection As ESRI.ArcGIS.Carto.IFeatureSelection=TryCast(featureLayer, ESRI.ArcGIS.Carto.IFeatureSelection) ' Dynamic Cast

  ' Set up the query
  Dim queryFilter As ESRI.ArcGIS.Geodatabase.IQueryFilter=New ESRI.ArcGIS.Geodatabase.QueryFilterClass
  queryFilter.WhereClause=whereClause

  ' Invalidate only the selection cache. Flag the original selection
  activeView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)

  ' Perform the selection
  featureSelection.SelectFeatures(queryFilter, ESRI.ArcGIS.Carto.esriSelectionResultEnum.esriSelectionResultNew, False)

  ' Flag the new selection
  activeView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)

End Sub

Additional Requirements
  • The code in this document requires the following References added to the Visual Studio project:
  • ESRI.ArcGIS.Carto
  • ESRI.ArcGIS.Geodatabase
  • ESRI.ArcGIS.Geometry
  • ESRI.ArcGIS.System