[C#]//ISelectionSet Example
//example uses a USA "states" feature class in a Personal Geodatabase
public void ISelectionSet_Example(IFeatureClass featureClass)
{
//This function creates various SelectionSets on the table and modifies them using the ISelectionSet methods.
IDataset dataset = (IDataset)featureClass;
//Set up query filter and use it to create the selection set.
IQueryFilter queryFilter = new QueryFilterClass();
queryFilter.WhereClause = "SUB_REGION = 'Pacific'";
//use the query filter to select features
ISelectionSet selectionSet = featureClass.Select(queryFilter, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, dataset.Workspace);
//use an IEnumIDs to read the SelectionSet IDs
IEnumIDs enumIDs = selectionSet.IDs;
int fieldIndex = featureClass.FindField("STATE_NAME");
string s = "{0}ID - STATE_NAME";
IFeature feature;int iD = enumIDs.Next();
while (iD != -1) //-1 is reutned after the last valid ID has been reached
{
feature = featureClass.GetFeature(iD);
s += "{0}" + iD + ": " + feature.get_Value(fieldIndex);
iD = enumIDs.Next();
}//report some information about the selection
Console.WriteLine("A SelectionSet containing: {1} Rows {0}has been created using the query: {2} {0}this selection set was created on the feature class: {3}{0}" + s + "{0}",Environment.NewLine, selectionSet.Count,queryFilter.WhereClause,dataset.Name);//add a list of features (OID's) to the selection set
System.Collections.Generic.List<int> constructOIDAddList = new System.Collections.Generic.List<int>();
constructOIDAddList.Add(22); // Nevada
constructOIDAddList.Add(23); // Utah
constructOIDAddList.Add(36); // Arizona
int[] oidAddList = constructOIDAddList.ToArray();//pass the oidList by reference
selectionSet.AddList(oidAddList.Length, ref oidAddList[0]);Console.WriteLine("{1} Features added to SelectionSet {0}{2} feature total in SelectionSet{0}", Environment.NewLine, oidAddList.Length.ToString(), selectionSet.Count);
//remove a list of feature (OID's) from the selection set
System.Collections.Generic.List<int> constructOIDDeleteList = new System.Collections.Generic.List<int>();
constructOIDDeleteList.Add(49); // Hawaii
constructOIDDeleteList.Add(50); // Alaska
int[] oidDeleteList = constructOIDDeleteList.ToArray();selectionSet.RemoveList(oidDeleteList.Length, ref oidDeleteList[0]);
Console.WriteLine("{1} Features removed from SelectionSet {0}{2} feature total in SelectionSet{0}", Environment.NewLine, oidDeleteList.Length.ToString(), selectionSet.Count);
//create second selection set by combining the first two
queryFilter.WhereClause = "SUB_REGION = 'W N Cen'";
ISelectionSet selectionSet2 = featureClass.Select(queryFilter, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, dataset.Workspace);//create a new selection set by combing the first two
ISelectionSet resultSet;
selectionSet.Combine(selectionSet2, esriSetOperation.esriSetUnion, out resultSet);
Console.WriteLine("A second SelectionSet containing: {1} Features {0}has been created using the query: {2} {0}this selection set has been combined with the existing SelectionSet {0}the resulting selection set resultSet contains: {3} Features{0}", Environment.NewLine, selectionSet2.Count, queryFilter.WhereClause, resultSet.Count);//create a third selection set by selecting from the SelectionSet containing the West North Centeral States
queryFilter.WhereClause = "STATE_NAME in ('Iowa', 'Minnesota', 'Missouri')"; //All of the states East of the Mississippi River
ISelectionSet selectionSet3 = selectionSet2.Select(queryFilter,esriSelectionType.esriSelectionTypeHybrid,esriSelectionOption.esriSelectionOptionNormal,dataset.Workspace);//create a new selection set by combining it with the one corresponding with the result of the first selection combine
//The operator esriSetSymDifference is a XOR so the content of the selectSet3 will be removed from the first set
resultSet.Combine(selectionSet3, esriSetOperation.esriSetSymDifference,out resultSet);
Console.WriteLine("A third SelectionSet containing: {1} Features {0}has been created using the query: {2} {0}this selection set has been removed from the existing SelectionSet {0}the resulting selection set resultSet contains: {3} Features{0}", Environment.NewLine, selectionSet3.Count, queryFilter.WhereClause, resultSet.Count);//create a FeatureCursor on the selection set to retrieve the selected records
queryFilter.WhereClause = "STATE_NAME like '*a'";
ICursor cursor;
resultSet.Search(queryFilter,false,out cursor);
IFeatureCursor featureCursor = (IFeatureCursor)cursor;s = null as string;
int recordCount = 0;
feature = featureCursor.NextFeature();
while (feature != null)
{
recordCount++;
s += "{0}" + feature.get_Value(fieldIndex);
feature = featureCursor.NextFeature();
}
Console.WriteLine("Using iSelectionSet::Search with the query: {1} {0}we find that the resultSet from the XOR combine {0}contains the following states with names ending in a: " + s, Environment.NewLine, queryFilter.WhereClause, featureClass.AliasName);
//Release the Cursors
System.Runtime.InteropServices.Marshal.ReleaseComObject(cursor);
System.Runtime.InteropServices.Marshal.ReleaseComObject(featureCursor);
}
[Visual Basic .NET, C++]
No example is available for Visual Basic .NET or C++. To view a Visual Basic 6.0 or C# example, click the Language Filter button
in the upper-left corner of the page.