ArcGIS Pro 3.2 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 QueryFilter()
    Public Function New()
    Example
    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);
    }
    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;
        }
      }
    }
    Requirements

    Target Platforms: Windows 11, Windows 10

    ArcGIS Pro version: 3 or higher.
    See Also