/// <summary>
        /// Since the LocalGovernment Geodatabase does not have AttributedRelationshipClasses, the following method illustrates the behavior
        /// if such a dataset existed.
        /// </summary>
        public void MainMethodCode()
        {
            // Opening a Non-Versioned SQL Server instance.

            DatabaseConnectionProperties connectionProperties = new DatabaseConnectionProperties(EnterpriseDatabaseType.SQLServer)
            {
                AuthenticationMode = AuthenticationMode.DBMS,

                // Where testMachine is the machine where the instance is running and testInstance is the name of the SqlServer instance.
                Instance = @"testMachine\testInstance",

                // Provided that a database called LocalGovernment has been created on the testInstance and geodatabase has been enabled on the database.
                Database = "LocalGovernment",

                // Provided that a login called gdb has been created and corresponding schema has been created with the required permissions.
                User     = "******",
                Password = "******",
                Version  = "dbo.DEFAULT"
            };

            using (Geodatabase geodatabase = new Geodatabase(connectionProperties))
                using (AttributedRelationshipClass attributedRelationshipClass = geodatabase.OpenDataset <AttributedRelationshipClass>("LocalGovernment.GDB.ParcelToBuilding"))
                    using (FeatureClass parcelFeatureClass = geodatabase.OpenDataset <FeatureClass>("LocalGovernment.GDB.Parcel"))
                        using (FeatureClass buildingFeatureClass = geodatabase.OpenDataset <FeatureClass>("LocalGovernment.GDB.Building"))
                        {
                            QueryFilter parcelQueryFilter = new QueryFilter {
                                WhereClause = "APN = 1234 OR APN = 5678"
                            };
                            QueryFilter buildingQueryFilter = new QueryFilter {
                                WhereClause = "BUILDID = 4321 OR BUILDID = 8765"
                            };

                            Selection parcelsSelection  = parcelFeatureClass.Select(parcelQueryFilter, SelectionType.ObjectID, SelectionOption.Normal);
                            Selection buildingSelection = buildingFeatureClass.Select(buildingQueryFilter, SelectionType.ObjectID, SelectionOption.Normal);

                            IReadOnlyList <AttributedRelationship> relationshipsForOrigin      = null;
                            IReadOnlyList <AttributedRelationship> relationshipsForDestination = null;

                            try
                            {
                                relationshipsForOrigin      = attributedRelationshipClass.GetRelationshipsForOriginRows(parcelsSelection.GetObjectIDs());
                                relationshipsForDestination = attributedRelationshipClass.GetRelationshipsForDestinationRows(buildingSelection.GetObjectIDs());
                            }
                            finally
                            {
                                Dispose(relationshipsForOrigin);
                                Dispose(relationshipsForDestination);
                            }
                        }
        }
        private async void ExcuteThis(double distance)
        {
            var result = await  ExtraObstacle(distance);

            //清除选项 以及选择该图层
            if (result != null && !result.IsFailed)
            {
                await QueuedTask.Run(() =>
                {
                    if (MapView.Active != null)
                    {
                        MapView.Active.Map.SetSelection(null);
                        Layer obstacle = MapView.Active.Map.GetLayersAsFlattenedList().FirstOrDefault(l => l.Name == ConstDefintion.ConstFeatureClass_CourseObstacle);
                        if (obstacle != null)
                        {
                            FeatureLayer fl = obstacle as FeatureLayer;
                            FeatureClass fc = fl.GetFeatureClass();
                            fl.SetSelection(fc.Select(null, SelectionType.ObjectID, SelectionOption.Normal));
                        }
                    }
                });
            }
        }
Example #3
0
        public void MainMethodCode()
        {
            using (Geodatabase fileGeodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(@"C:\Data\LocalGovernment.gdb"))))
                using (Table table = fileGeodatabase.OpenDataset <Table>("EmployeeInfo"))
                {
                    QueryFilter anotherQueryFilter = new QueryFilter {
                        WhereClause = "COSTCTRN = 'Information Technology'"
                    };

                    using (Selection itFolks = table.Select(anotherQueryFilter, SelectionType.ObjectID, SelectionOption.Normal))
                    {
                        long firstItGuy = itFolks.GetObjectIDs().First();
                        itFolks.Remove(new List <long> {
                            firstItGuy
                        });
                    }
                }

            // Opening a Non-Versioned SQL Server instance.

            DatabaseConnectionProperties connectionProperties = new DatabaseConnectionProperties(EnterpriseDatabaseType.SQLServer)
            {
                AuthenticationMode = AuthenticationMode.DBMS,

                // Where testMachine is the machine where the instance is running and testInstance is the name of the SqlServer instance.
                Instance = @"testMachine\testInstance",

                // Provided that a database called LocalGovernment has been created on the testInstance and geodatabase has been enabled on the database.
                Database = "LocalGovernment",

                // Provided that a login called gdb has been created and corresponding schema has been created with the required permissions.
                User     = "******",
                Password = "******",
                Version  = "dbo.DEFAULT"
            };

            using (Geodatabase geodatabase = new Geodatabase(connectionProperties))
                using (FeatureClass featureClass = geodatabase.OpenDataset <FeatureClass>("LocalGovernment.GDB.SchoolBoundary"))
                {
                    QueryFilter filter = new QueryFilter {
                        WhereClause = "DISTRCTNAME = 'Indian Prairie School District 204'"
                    };

                    // Using SpatialQueryFilter with Selection.Select.
                    SpatialQueryFilter spatialQueryFilter = new SpatialQueryFilter
                    {
                        FilterGeometry = new PolygonBuilder(new List <Coordinate2D>
                        {
                            new Coordinate2D(1021880, 1867396),
                            new Coordinate2D(1028223, 1870705),
                            new Coordinate2D(1031165, 1866844),
                            new Coordinate2D(1025373, 1860501),
                            new Coordinate2D(1021788, 1863810),
                        }).ToGeometry(),

                        SpatialRelationship = SpatialRelationship.Intersects
                    };

                    using (Selection indianPrairieSelection = featureClass.Select(filter, SelectionType.ObjectID, SelectionOption.Normal))
                        using (Selection emptySelection = indianPrairieSelection.Select(new QueryFilter(), SelectionOption.Empty))
                            using (Selection spatialSelection = indianPrairieSelection.Select(spatialQueryFilter))
                            {
                                // One does not have to worry about whether the ObjectIds being removed are present in the selection. Non-existent Object Ids are ignored.
                                indianPrairieSelection.Remove(spatialSelection.GetObjectIDs());

                                // For the same reason as above, removing from an enpty selection will not fail.
                                emptySelection.Remove(spatialSelection.GetObjectIDs());

                                Selection selection       = featureClass.Select(null, SelectionType.ObjectID, SelectionOption.Normal);
                                long      invalidObjectId = GetHighestObjectId(selection.GetObjectIDs()) + 1;

                                // Even removing invalid opbject ids will not fail.
                                selection.Remove(new List <long> {
                                    invalidObjectId
                                });

                                try
                                {
                                    selection.Remove(new List <long> {
                                        -1
                                    });
                                }
                                catch (ArgumentException exception)
                                {
                                    // Negative object Ids are not allowed.
                                }
                            }
                }
        }
        public void MainMethodCode()
        {
            using (Geodatabase fileGeodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(@"C:\Data\LocalGovernment.gdb"))))
                using (FeatureClass featureClass = fileGeodatabase.OpenDataset <FeatureClass>("PollingPlace"))
                {
                    QueryFilter queryFilter = new QueryFilter {
                        WhereClause = "CITY = 'Plainfield'"
                    };

                    // For Selecting all matching entries.
                    Selection selection = featureClass.Select(queryFilter, SelectionType.ObjectID, SelectionOption.Normal);

                    // Number of records that match the QueryFilter criteria.
                    int numberOfMatchingRecords = selection.GetCount();

                    // The objectIDs which match the QueryFilter criteria.
                    IReadOnlyList <long> readOnlyList = selection.GetObjectIDs();
                }

            // Opening a Non-Versioned SQL Server instance.

            DatabaseConnectionProperties connectionProperties = new DatabaseConnectionProperties(EnterpriseDatabaseType.SQLServer)
            {
                AuthenticationMode = AuthenticationMode.DBMS,

                // Where testMachine is the machine where the instance is running and testInstance is the name of the SqlServer instance.
                Instance = @"testMachine\testInstance",

                // Provided that a database called LocalGovernment has been created on the testInstance and geodatabase has been enabled on the database.
                Database = "LocalGovernment",

                // Provided that a login called gdb has been created and corresponding schema has been created with the required permissions.
                User     = "******",
                Password = "******",
                Version  = "dbo.DEFAULT"
            };

            using (Geodatabase geodatabase = new Geodatabase(connectionProperties))
                using (FeatureClass enterpriseFeatureClass = geodatabase.OpenDataset <FeatureClass>("LocalGovernment.GDB.FacilitySite"))
                {
                    List <Coordinate2D> newCoordinates = new List <Coordinate2D>
                    {
                        new Coordinate2D(1021570, 1880583),
                        new Coordinate2D(1028730, 1880994),
                        new Coordinate2D(1029718, 1875644),
                        new Coordinate2D(1021405, 1875397)
                    };

                    SpatialQueryFilter spatialFilter = new SpatialQueryFilter
                    {
                        WhereClause         = "FCODE = 'Park'",
                        FilterGeometry      = new PolygonBuilder(newCoordinates).ToGeometry(),
                        SpatialRelationship = SpatialRelationship.Crosses
                    };

                    // For Selecting all matching entries.
                    Selection anotherSelection = enterpriseFeatureClass.Select(spatialFilter, SelectionType.ObjectID, SelectionOption.Normal);

                    // This can be used to get one record which matches the criteria. No assumptions can be made about which record satisfying the
                    // criteria is selected.
                    Selection onlyOneSelection = enterpriseFeatureClass.Select(spatialFilter, SelectionType.ObjectID, SelectionOption.OnlyOne);

                    // This will always be one.
                    int singleRecordCount = onlyOneSelection.GetCount();

                    // This will always have one record.
                    IReadOnlyList <long> listWithOneValue = onlyOneSelection.GetObjectIDs();

                    // This can be used to obtain a empty selction which can be used as a container to combine results from different selections.
                    Selection emptySelection = enterpriseFeatureClass.Select(spatialFilter, SelectionType.ObjectID, SelectionOption.Empty);

                    // This will always be zero.
                    int zeroCount = emptySelection.GetCount();

                    // This will always have zero record.
                    IReadOnlyList <long> emptyList = emptySelection.GetObjectIDs();

                    // If you want to select all the records in a table.
                    Selection allRecordSelection = enterpriseFeatureClass.Select(null, SelectionType.ObjectID, SelectionOption.Normal);

                    // This will give the count of records in a table.
                    int numberOfRecordsInATable = allRecordSelection.GetCount();
                }
        }
Example #5
0
        public void MainMethodCode()
        {
            using (Geodatabase fileGeodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(@"C:\Data\LocalGovernment.gdb"))))
                using (FeatureClass featureClass = fileGeodatabase.OpenDataset <FeatureClass>("SchoolBoundary"))
                {
                    SpatialQueryFilter spatialQueryFilter = new SpatialQueryFilter
                    {
                        FilterGeometry = new PolygonBuilder(new List <Coordinate2D>
                        {
                            new Coordinate2D(1021880, 1867396),
                            new Coordinate2D(1028223, 1870705),
                            new Coordinate2D(1031165, 1866844),
                            new Coordinate2D(1025373, 1860501),
                            new Coordinate2D(1021788, 1863810),
                        }).ToGeometry(),

                        SpatialRelationship = SpatialRelationship.Intersects
                    };

                    QueryFilter anotherQueryFilter = new QueryFilter {
                        WhereClause = "DISTRCTNAME = 'Indian Prairie School District 204'"
                    };

                    using (Selection spatialSelection = featureClass.Select(spatialQueryFilter, SelectionType.ObjectID, SelectionOption.Normal))
                        using (Selection indianPrairieSelection = featureClass.Select(anotherQueryFilter, SelectionType.ObjectID, SelectionOption.Normal))
                        {
                            // In order to find all features which are in the specified Polygon and in the specified district.
                            using (Selection selection = spatialSelection.Combine(indianPrairieSelection, SetOperation.Intersection))
                            {
                                IEnumerable <long> oids = indianPrairieSelection.GetObjectIDs();
                                selection.Add(oids.ToList());
                            }
                        }
                }

            //Opening a Non-Versioned SQL Server instance
            DatabaseConnectionProperties connectionProperties = new DatabaseConnectionProperties(EnterpriseDatabaseType.SQLServer)
            {
                AuthenticationMode = AuthenticationMode.DBMS,

                // Where testMachine is the machine where the instance is running and testInstance is the name of the SqlServer instance.
                Instance = @"testMachine\testInstance",

                // Provided that a database called LocalGovernment has been created on the testInstance and geodatabase has been enabled on the database.
                Database = "LocalGovernment",

                // Provided that a login called gdb has been created and corresponding schema has been created with the required permissions.
                User     = "******",
                Password = "******",
                Version  = "dbo.DEFAULT"
            };

            using (Geodatabase geodatabase = new Geodatabase(connectionProperties))
                using (FeatureClass facilitySiteFeatureClass = geodatabase.OpenDataset <FeatureClass>("LocalGovernment.GDB.FacilitySite"))
                {
                    QueryFilter queryFilter = new QueryFilter {
                        WhereClause = "FCODE = 'Park'"
                    };
                    QueryFilter subtypeQueryFilter = new QueryFilter {
                        WhereClause = "SUBTYPEFIELD = 730"
                    };
                    using (Selection parkSelection = facilitySiteFeatureClass.Select(queryFilter, SelectionType.ObjectID, SelectionOption.Normal))
                        using (Selection educationSubtypeSelection = facilitySiteFeatureClass.Select(subtypeQueryFilter, SelectionType.ObjectID, SelectionOption.Normal))
                            using (Selection parksAndSchools = educationSubtypeSelection.Combine(parkSelection, SetOperation.Union))
                            {
                            }

                    QueryFilter hazardousFacilityFilter = new QueryFilter {
                        WhereClause = "FCODE = 'Hazardous Materials Facility'"
                    };
                    QueryFilter industrySubtypeQueryFilter = new QueryFilter {
                        WhereClause = "SUBTYPEFIELD = 710"
                    };
                    using (Selection hazardousFacilitySelection = facilitySiteFeatureClass.Select(hazardousFacilityFilter, SelectionType.ObjectID, SelectionOption.Normal))
                        using (Selection industrySelection = facilitySiteFeatureClass.Select(industrySubtypeQueryFilter, SelectionType.ObjectID, SelectionOption.Normal))
                            using (Selection nonHazardousIndustryFacilities = industrySelection.Combine(hazardousFacilitySelection, SetOperation.Difference))
                            {
                            }

                    QueryFilter nonProfitFilter = new QueryFilter {
                        WhereClause = "OWNTYPE = 'Non-Profit'"
                    };
                    QueryFilter publicAttractionQueryFilter = new QueryFilter {
                        WhereClause = "SUBTYPEFIELD = 820"
                    };
                    using (Selection nonProfitSelection = facilitySiteFeatureClass.Select(nonProfitFilter, SelectionType.ObjectID, SelectionOption.Normal))
                        using (Selection publicAttractionSelection = facilitySiteFeatureClass.Select(publicAttractionQueryFilter, SelectionType.ObjectID, SelectionOption.Normal))
                            using (Selection eitherPublicAttactionOrNonProfit = nonProfitSelection.Combine(publicAttractionSelection, SetOperation.SymmetricDifference))
                            {
                            }
                }
        }
        public void MainMethodCode()
        {
            using (Geodatabase fileGeodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(@"C:\Data\LocalGovernment.gdb"))))
                using (Table table = fileGeodatabase.OpenDataset <Table>("EmployeeInfo"))
                    using (Selection selection = table.Select(null, SelectionType.ObjectID, SelectionOption.Normal))
                    {
                        QueryFilter queryFilter = new QueryFilter {
                            WhereClause = "COSTCTRN = 'Finance'"
                        };

                        // An existing selection can be further filtered to get another selection as below.
                        using (Selection newSelection = selection.Select(queryFilter))
                        {
                        }
                    }

            // Opening a Non-Versioned SQL Server instance.

            DatabaseConnectionProperties connectionProperties = new DatabaseConnectionProperties(EnterpriseDatabaseType.SQLServer)
            {
                AuthenticationMode = AuthenticationMode.DBMS,

                // Where testMachine is the machine where the instance is running and testInstance is the name of the SqlServer instance.
                Instance = @"testMachine\testInstance",

                // Provided that a database called LocalGovernment has been created on the testInstance and geodatabase has been enabled on the database.
                Database = "LocalGovernment",

                // Provided that a login called gdb has been created and corresponding schema has been created with the required permissions.
                User     = "******",
                Password = "******",
                Version  = "dbo.DEFAULT"
            };

            using (Geodatabase geodatabase = new Geodatabase(connectionProperties))
                using (FeatureClass featureClass = geodatabase.OpenDataset <FeatureClass>("LocalGovernment.GDB.SchoolBoundary"))
                {
                    QueryFilter filter = new QueryFilter {
                        WhereClause = "DISTRCTNAME = 'Indian Prairie School District 204'"
                    };

                    // The following can be used to find one record out of the selection which will satisfy a clause.
                    QueryFilter areaFilter = new QueryFilter {
                        WhereClause = "SCHOOLAREA > 0.5"
                    };

                    using (Selection indianPrairieSelection = featureClass.Select(filter, SelectionType.ObjectID, SelectionOption.Normal))
                        using (Selection selectionWithOneSchoolWithDesiredArea = indianPrairieSelection.Select(areaFilter, SelectionOption.OnlyOne))
                        {
                            // Similar to the Table.Select, one can get an empty selection using the Empty Option.
                            using (Selection emptySelection = indianPrairieSelection.Select(areaFilter, SelectionOption.Empty))
                            {
                            }

                            // Using SpatialQueryFilter with Selection.Select.
                            SpatialQueryFilter spatialQueryFilter = new SpatialQueryFilter
                            {
                                FilterGeometry = new PolygonBuilder(new List <Coordinate2D>
                                {
                                    new Coordinate2D(1021880, 1867396),
                                    new Coordinate2D(1028223, 1870705),
                                    new Coordinate2D(1031165, 1866844),
                                    new Coordinate2D(1025373, 1860501),
                                    new Coordinate2D(1021788, 1863810)
                                }).ToGeometry(),

                                SpatialRelationship = SpatialRelationship.Intersects
                            };

                            using (Selection spatialSelection = indianPrairieSelection.Select(spatialQueryFilter))
                            {
                            }
                        }
                }
        }
        public void MainMethodCode()
        {
            using (Geodatabase fileGeodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(@"C:\Data\LocalGovernment.gdb"))))
                using (FeatureClass featureClass = fileGeodatabase.OpenDataset <FeatureClass>("PollingPlace"))
                {
                    QueryFilter queryFilter = new QueryFilter {
                        WhereClause = "CITY = 'Plainfield'"
                    };
                    Selection selection = featureClass.Select(queryFilter, SelectionType.ObjectID, SelectionOption.Normal);

                    // The result is a mapping between those object ids which failed validation and the reason why the validation failed (a string message).
                    IReadOnlyDictionary <long, string> validationResult = featureClass.Validate(selection);

                    RowCursor      rowCursor = featureClass.Search(queryFilter, false);
                    List <Feature> features  = new List <Feature>();

                    try
                    {
                        while (rowCursor.MoveNext())
                        {
                            features.Add(rowCursor.Current as Feature);
                        }

                        // This is equivalent to the validation performed using the selection.
                        IReadOnlyDictionary <long, string> equivalentValidationResult = featureClass.Validate(features);

                        // Again this is equivalent to both the above results.
                        IReadOnlyDictionary <long, string> anotherEquivalentResult = featureClass.Validate(queryFilter);

                        SpatialQueryFilter spatialQueryFilter = new SpatialQueryFilter
                        {
                            FilterGeometry = new EnvelopeBuilder(
                                new MapPointBuilder(1052803, 1812751).ToGeometry(),
                                new MapPointBuilder(1034600, 1821320).ToGeometry()).ToGeometry(),

                            SpatialRelationship = SpatialRelationship.Within
                        };

                        IReadOnlyDictionary <long, string> spatialFilterBasedValidationResult = featureClass.Validate(spatialQueryFilter);
                    }
                    finally
                    {
                        rowCursor.Dispose();
                        Dispose(features);
                    }
                }

            // Opening a Non-Versioned SQL Server instance.

            DatabaseConnectionProperties connectionProperties = new DatabaseConnectionProperties(EnterpriseDatabaseType.SQLServer)
            {
                AuthenticationMode = AuthenticationMode.DBMS,

                // Where testMachine is the machine where the instance is running and testInstance is the name of the SqlServer instance.
                Instance = @"testMachine\testInstance",

                // Provided that a database called LocalGovernment has been created on the testInstance and geodatabase has been enabled on the database.
                Database = "LocalGovernment",

                // Provided that a login called gdb has been created and corresponding schema has been created with the required permissions.
                User     = "******",
                Password = "******",
                Version  = "dbo.DEFAULT"
            };

            using (Geodatabase geodatabase = new Geodatabase(connectionProperties))
                using (FeatureClass enterpriseFeatureClass = geodatabase.OpenDataset <FeatureClass>("LocalGovernment.GDB.FacilitySite"))
                {
                    QueryFilter parkFilter = new QueryFilter {
                        WhereClause = "FCODE = 'Park'"
                    };
                    Selection parkSelection = enterpriseFeatureClass.Select(parkFilter, SelectionType.ObjectID, SelectionOption.Normal);

                    // Remember that validation cost is directly proprtional to the number of Features validated. So, select the set of features to be
                    // validated judiciously.  This will be empty because all the Park Features are valid.
                    IReadOnlyDictionary <long, string> emptyDictionary = enterpriseFeatureClass.Validate(parkSelection);

                    // We are adding an invalid feature to illustrate a case where the validation result is not empty.

                    long invalidFeatureObjectID = -1;

                    EditOperation editOperation = new EditOperation();
                    editOperation.Callback(context =>
                    {
                        RowBuffer rowBuffer = null;
                        Feature feature     = null;

                        try
                        {
                            FeatureClassDefinition facilitySiteDefinition = enterpriseFeatureClass.GetDefinition();

                            rowBuffer = enterpriseFeatureClass.CreateRowBuffer();

                            rowBuffer["FACILITYID"] = "FAC-400";
                            rowBuffer["NAME"]       = "Griffith Park";
                            rowBuffer["OWNTYPE"]    = "Municipal";
                            rowBuffer["FCODE"]      = "Park";
                            // Note that this is an invalid subtype value.
                            rowBuffer[facilitySiteDefinition.GetSubtypeField()] = 890;
                            rowBuffer[facilitySiteDefinition.GetShapeField()]   = new PolygonBuilder(new List <Coordinate2D>
                            {
                                new Coordinate2D(1021570, 1880583),
                                new Coordinate2D(1028730, 1880994),
                                new Coordinate2D(1029718, 1875644),
                                new Coordinate2D(1021405, 1875397)
                            }).ToGeometry();

                            feature = enterpriseFeatureClass.CreateRow(rowBuffer);

                            invalidFeatureObjectID = feature.GetObjectID();
                        }
                        catch (GeodatabaseException exObj)
                        {
                            Console.WriteLine(exObj);
                        }
                        finally
                        {
                            if (rowBuffer != null)
                            {
                                rowBuffer.Dispose();
                            }

                            if (feature != null)
                            {
                                feature.Dispose();
                            }
                        }
                    }, enterpriseFeatureClass);

                    editOperation.Execute();

                    // This will have one keypair value for the invalid row that was added.
                    IReadOnlyDictionary <long, string> result = enterpriseFeatureClass.Validate(parkFilter);

                    // This will say "invalid subtype code".
                    string errorMessage = result[invalidFeatureObjectID];
                }
        }
Example #8
0
        public void MainMethodCode()
        {
            using (Geodatabase fileGeodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(@"C:\Data\LocalGovernment.gdb"))))
                using (Table table = fileGeodatabase.OpenDataset <Table>("EmployeeInfo"))
                {
                    QueryFilter queryFilter = new QueryFilter {
                        WhereClause = "COSTCTRN = 'Finance'"
                    };
                    QueryFilter anotherQueryFilter = new QueryFilter {
                        WhereClause = "COSTCTRN = 'Information Technology'"
                    };

                    using (Selection onlyOneFinance = table.Select(queryFilter, SelectionType.ObjectID, SelectionOption.OnlyOne))
                        using (Selection itFolks = table.Select(anotherQueryFilter, SelectionType.ObjectID, SelectionOption.Normal))
                        {
                            itFolks.Add(onlyOneFinance.GetObjectIDs());
                        }
                }

            // Opening a Non-Versioned SQL Server instance.

            DatabaseConnectionProperties connectionProperties = new DatabaseConnectionProperties(EnterpriseDatabaseType.SQLServer)
            {
                AuthenticationMode = AuthenticationMode.DBMS,

                // Where testMachine is the machine where the instance is running and testInstance is the name of the SqlServer instance.
                Instance = @"testMachine\testInstance",

                // Provided that a database called LocalGovernment has been created on the testInstance and geodatabase has been enabled on the database.
                Database = "LocalGovernment",

                // Provided that a login called gdb has been created and corresponding schema has been created with the required permissions.
                User     = "******",
                Password = "******",
                Version  = "dbo.DEFAULT"
            };

            QueryFilter filter = new QueryFilter {
                WhereClause = "DISTRCTNAME = 'Indian Prairie School District 204'"
            };

            using (Geodatabase geodatabase = new Geodatabase(connectionProperties))
                using (FeatureClass featureClass = geodatabase.OpenDataset <FeatureClass>("LocalGovernment.GDB.SchoolBoundary"))
                    using (Selection indianPrairieSelection = featureClass.Select(filter, SelectionType.ObjectID, SelectionOption.Normal))
                    {
                        // Although the Add method can be used with Normal and OnlyOne selections, Empty selections are the ones which will absolutely need Add method.
                        using (Selection emptySelection = indianPrairieSelection.Select(new QueryFilter(), SelectionOption.Empty))
                        {
                            // Using SpatialQueryFilter with Selection.Select.
                            SpatialQueryFilter spatialQueryFilter = new SpatialQueryFilter
                            {
                                FilterGeometry = new PolygonBuilder(new List <Coordinate2D>
                                {
                                    new Coordinate2D(1021880, 1867396),
                                    new Coordinate2D(1028223, 1870705),
                                    new Coordinate2D(1031165, 1866844),
                                    new Coordinate2D(1025373, 1860501),
                                    new Coordinate2D(1021788, 1863810)
                                }).ToGeometry(),

                                SpatialRelationship = SpatialRelationship.Intersects
                            };

                            //Another SpatialQueryFilter
                            SpatialQueryFilter anotherSpatialQueryFilter = new SpatialQueryFilter
                            {
                                FilterGeometry = new PolygonBuilder(new List <Coordinate2D>
                                {
                                    new Coordinate2D(1015106, 1829421),
                                    new Coordinate2D(1013413, 1834760),
                                    new Coordinate2D(1020705, 1840359),
                                    new Coordinate2D(1026304, 1838145)
                                }).ToGeometry(),

                                SpatialRelationship = SpatialRelationship.Intersects
                            };

                            using (Selection spatialSelection = indianPrairieSelection.Select(spatialQueryFilter))
                                using (Selection anotherSpatialSelection = indianPrairieSelection.Select(anotherSpatialQueryFilter))
                                {
                                    //Now that we have two selections, we can add the ObjectIds of these selections to the empty selection without affecting the original selections
                                    //Add does not add object ids which are already present in the selection. So, there should be no exceptions or duplicate Ids.

                                    emptySelection.Add(spatialSelection.GetObjectIDs());
                                    emptySelection.Add(anotherSpatialSelection.GetObjectIDs());

                                    Selection selection       = featureClass.Select(null, SelectionType.ObjectID, SelectionOption.Normal);
                                    long      invalidObjectId = GetHighestObjectId(selection.GetObjectIDs()) + 1;

                                    // This will not throw an exception or fail. Further, the invalid ids will not be added to the selection.
                                    selection.Add(new List <long> {
                                        invalidObjectId
                                    });
                                }
                        }
                    }
        }
        public void MainMethodCode()
        {
            using (Geodatabase fileGeodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(@"C:\Data\LocalGovernment.gdb"))))
                using (Table table = fileGeodatabase.OpenDataset <Table>("EmployeeInfo"))
                {
                    QueryFilter anotherQueryFilter = new QueryFilter {
                        WhereClause = "COSTCTRN = 'Information Technology'"
                    };
                    using (Selection itFolks = table.Select(anotherQueryFilter, SelectionType.ObjectID, SelectionOption.Normal))
                    {
                        // Search with a null or a QueryFilter with an empty where clause will return all the Rows for the ObjectIds.
                        // This can be used to obtain the rows for all the ObjectIds in the selection.

                        using (RowCursor rowCursor = itFolks.Search(null, false))
                        {
                            while (rowCursor.MoveNext())
                            {
                                using (Row row = rowCursor.Current)
                                {
                                    // Process the row.
                                }
                            }
                        }
                    }
                }

            // Opening a Non-Versioned SQL Server instance.

            DatabaseConnectionProperties connectionProperties = new DatabaseConnectionProperties(EnterpriseDatabaseType.SQLServer)
            {
                AuthenticationMode = AuthenticationMode.DBMS,

                // Where testMachine is the machine where the instance is running and testInstance is the name of the SqlServer instance.
                Instance = @"testMachine\testInstance",

                // Provided that a database called LocalGovernment has been created on the testInstance and geodatabase has been enabled on the database.
                Database = "LocalGovernment",

                // Provided that a login called gdb has been created and corresponding schema has been created with the required permissions.
                User     = "******",
                Password = "******",
                Version  = "dbo.DEFAULT"
            };

            using (Geodatabase geodatabase = new Geodatabase(connectionProperties))
                using (FeatureClass featureClass = geodatabase.OpenDataset <FeatureClass>("LocalGovernment.GDB.SchoolBoundary"))
                {
                    QueryFilter indianPrairieFilter = new QueryFilter {
                        WhereClause = "DISTRCTNAME = 'Indian Prairie School District 204'"
                    };
                    using (Selection indianPrairieSelection = featureClass.Select(indianPrairieFilter, SelectionType.ObjectID, SelectionOption.Normal))
                    {
                        // Using SpatialQueryFilter with Selection.Search.
                        SpatialQueryFilter spatialQueryFilter = new SpatialQueryFilter
                        {
                            FilterGeometry = new PolygonBuilder(new List <Coordinate2D>
                            {
                                new Coordinate2D(1021880, 1867396),
                                new Coordinate2D(1028223, 1870705),
                                new Coordinate2D(1031165, 1866844),
                                new Coordinate2D(1025373, 1860501),
                                new Coordinate2D(1021788, 1863810)
                            }).ToGeometry(),

                            SpatialRelationship = SpatialRelationship.Intersects
                        };

                        // Note that we are using Recycling.
                        using (RowCursor spatialSearch = indianPrairieSelection.Search(spatialQueryFilter, true))
                        {
                            // Note that we are calling the FindField outside the loop because this searches all the fields. There is no point in performing it for every Row.
                            int nameFieldIndex = spatialSearch.FindField("NAME");

                            while (spatialSearch.MoveNext())
                            {
                                using (Row row = spatialSearch.Current)
                                {
                                    Console.WriteLine(row[nameFieldIndex]);
                                }
                            }
                        }

                        QueryFilter queryFilter = new QueryFilter {
                            WhereClause = "NAME LIKE '///%Elementary///%'"
                        };
                        using (Selection elementarySelection = featureClass.Select(queryFilter, SelectionType.ObjectID, SelectionOption.Normal))
                        {
                            // Note that the cursor will contain only those rows which match "NAME LIKE '%Elementary%'" and "DISTRCTNAME = 'Indian Prairie School District 204'".
                            // This is to illustrate that the seach will only happen on the object Ids in the selection.
                            using (RowCursor cursor = elementarySelection.Search(indianPrairieFilter, false))
                            {
                                while (cursor.MoveNext())
                                {
                                    using (Feature feature = (Feature)cursor.Current)
                                    {
                                        // Process feature.
                                    }
                                }
                            }
                        }

                        using (Selection onlyOneIndianPrairie = indianPrairieSelection.Select(null, SelectionOption.OnlyOne))
                        {
                            QueryFilter napervilleFilter = new QueryFilter {
                                WhereClause = "DISTRCTNAME LIKE '///%Naperville///%'"
                            };

                            using (RowCursor napervilleSearch = onlyOneIndianPrairie.Search(napervilleFilter, false))
                            {
                                // This will be false because there are no object ids in onlyOneIndianPrairie which have NaperVille in their DistrictName.
                                bool hasRow = napervilleSearch.MoveNext();

                                using (Selection emptySelection = indianPrairieSelection.Select(null, SelectionOption.Empty))
                                    using (RowCursor searchOnEmptySelection = emptySelection.Search(napervilleFilter, false))
                                    {
                                        // Again, this will be false because there are no object ids in emptySelection.
                                        hasRow = searchOnEmptySelection.MoveNext();
                                    }
                            }
                        }
                    }
                }
        }