Represents a filter for performing a query against a
Table.
Searching a Table using QueryFilter
public async Task SearchingATable()
{
try
{
await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde"))))
using (Table table = geodatabase.OpenDataset<Table>("EmployeeInfo"))
{
QueryFilter queryFilter = new QueryFilter
{
WhereClause = "COSTCTRN = 'Information Technology'",
SubFields = "KNOWNAS, OFFICE, LOCATION",
PostfixClause = "ORDER BY OFFICE"
};
using (RowCursor rowCursor = table.Search(queryFilter, false))
{
while (rowCursor.MoveNext())
{
using (Row row = rowCursor.Current)
{
string location = Convert.ToString(row["LOCATION"]);
string knownAs = Convert.ToString(row["KNOWNAS"]);
}
}
}
}
});
}
catch (GeodatabaseFieldException fieldException)
{
// One of the fields in the where clause might not exist. There are multiple ways this can be handled:
// Handle error appropriately
}
catch (Exception exception)
{
// logger.Error(exception.Message);
}
}
Searching a Table for non-Latin characters
using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde"))))
using (Table table = geodatabase.OpenDataset<Table>("TableWithChineseCharacters"))
{
// This will fail with many database systems that expect Latin characters by default
string incorrectWhereClause = "颜色 = '绿'";
// Correct solution is to prepend the 'National String Prefix' to the attribute value
// For example, with SQL Server this value is 'N'
// This value is obtained using the SQLSyntax class
string nationalStringPrefix = "";
SQLSyntax sqlSyntax = geodatabase.GetSQLSyntax();
nationalStringPrefix = sqlSyntax.GetSupportedStrings(SQLStringType.NationalStringPrefix).First();
// This Where clause will work
QueryFilter queryFilter = new QueryFilter()
{
WhereClause = "颜色 = " + nationalStringPrefix + "'绿'"
};
}
Searching a Table using a set of ObjectIDs
public RowCursor SearchingATable(Table table, IReadOnlyList<long> objectIDs)
{
QueryFilter queryFilter = new QueryFilter()
{
ObjectIDs = objectIDs
};
return table.Search(queryFilter);
}
Pagination in QueryFilter
public void QueryFilterWithPagination(Table table, List<long> objectIDs)
{
int rowsPerBatch = 100;
int offset = 0;
// Query filter
// Some datastores support pagination only through an SQL postfix clause
QueryFilter queryFilter = new QueryFilter()
{
ObjectIDs = objectIDs,
PostfixClause = "ORDER BY OBJECTID"
};
// Fetch rows in a batch from a table
for (int index = offset; index <= objectIDs.Count; index += rowsPerBatch)
{
// Set number of rows to return from a table
queryFilter.RowCount = rowsPerBatch;
// Set positional offset to skip number of rows from a table
queryFilter.Offset = index;
using (RowCursor cursor = table.Search(queryFilter))
{
while (cursor.MoveNext())
{
using (Row row = cursor.Current)
{
Console.WriteLine(row.GetObjectID());
}
}
}
}
}
Fetching a Row from an Element
// usage : using (var row = FetchRowFromElement(...))
public static Row FetchRowFromElement(UtilityNetwork utilityNetwork, Element element)
{
// Get the table from the element
using (Table table = utilityNetwork.GetTable(element.NetworkSource))
{
// Create a query filter to fetch the appropriate row
QueryFilter queryFilter = new QueryFilter()
{
ObjectIDs = new List<long>() { element.ObjectID }
};
// Fetch and return the row
using (RowCursor rowCursor = table.Search(queryFilter))
{
if (rowCursor.MoveNext())
{
return rowCursor.Current;
}
return null;
}
}
}
Target Platforms: Windows 11, Windows 10
ArcGIS Pro version: 3 or higher.