ArcGIS Pro 3.7 API Reference Guide
ArcGIS.Core.Data Namespace / QueryFilter Class / QueryFilter Constructor
Example

In This Topic
    QueryFilter Constructor
    In This Topic
    Represents a filter for performing a query against a Table.
    Syntax
    Public Function New()
    public QueryFilter()
    Example
    Searching a Table using QueryFilter
    {
      await QueuedTask.Run(() =>
      {
        try
        {
          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
        }
      });
    }
    Searching a Table for non-Latin characters
    {
      await QueuedTask.Run(() =>
      {
        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
    {
      //Must be called within QueuedTask.Run()
      RowCursor SearchingATable(Table table, IReadOnlyList<long> objectIDs)
      {
        QueryFilter queryFilter = new QueryFilter()
        {
          ObjectIDs = objectIDs
        };
    
        return table.Search(queryFilter);
      }
    }
    Pagination in QueryFilter
    {
      // Must be called within QueuedTask.Run
      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
    {
      static Row FetchRowFromElement(UtilityNetwork utilityNetwork, Element element)
      {
        // Get the table from the element
        using (Table unTable = 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 = unTable.Search(queryFilter))
          {
            if (rowCursor.MoveNext())
            {
              return rowCursor.Current;
            }
            return null;
          }
        }
      }
    }
    Requirements

    Target Platforms: Windows 11 Home, Pro, Enterprise (64 bit)

    ArcGIS Pro version: 3.0 or higher.
    See Also