ArcGIS Pro 3.0 API Reference Guide
ArcGIS.Core.Data Namespace / QueryDef Class
Members Example

In This Topic
    QueryDef Class
    In This Topic
    Provides a capability to create virtual tables that represent queries involving one or more tables from the same geodatabase.
    Syntax
    public class QueryDef 
    Public Class QueryDef 
    Example
    Evaluating a QueryDef on a single table
    public async Task SimpleQueryDef()
    {
      await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
      {
        using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file"))))
        {
          QueryDef adaCompilantParksQueryDef = new QueryDef
          {
            Tables = "Park",
            WhereClause = "ADACOMPLY = 'Yes'",
          };
    
          using (RowCursor rowCursor = geodatabase.Evaluate(adaCompilantParksQueryDef, false))
          {
            while (rowCursor.MoveNext())
            {
              using (Row row = rowCursor.Current)
              {
                Feature feature = row as Feature;
                Geometry shape = feature.GetShape();
    
                String type = Convert.ToString(row["ADACOMPLY"]); // will be "Yes" for each row.
    
                try
                {
                  Table table = row.GetTable(); // Will always throw exception.
                }
                catch (NotSupportedException exception)
                {
                  // Handle not supported exception.
                }
              }
            }
          }
        }
      });
    }
    Evaluating a QueryDef on a Join using WHERE Clause
    public async Task JoiningWithWhereQueryDef()
    {
      await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
      {
        using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file"))))
        {
          QueryDef municipalEmergencyFacilitiesQueryDef = new QueryDef
          {
            SubFields = "EmergencyFacility.OBJECTID, EmergencyFacility.Shape, EmergencyFacility.FACILITYID, FacilitySite.FACILITYID, FacilitySite.FCODE",
            Tables = "EmergencyFacility, FacilitySite",
            WhereClause = "EmergencyFacility.FACNAME = FacilitySite.NAME AND EmergencyFacility.JURISDICT = 'Municipal'",
          };
    
          using (RowCursor rowCursor = geodatabase.Evaluate(municipalEmergencyFacilitiesQueryDef, false))
          {
            while (rowCursor.MoveNext())
            {
              using (Row row = rowCursor.Current)
              {
                Feature feature = row as Feature;
                Geometry shape = feature.GetShape();
    
                long objectID = Convert.ToInt64(row["EmergencyFacility.OBJECTID"]);
                String featureCode = Convert.ToString(row["FacilitySite.FCODE"]);
    
                IReadOnlyList<Field> fields = feature.GetFields(); //Contains one ArcGIS.Core.Data.Field objects for every subfield
              }
            }
          }
        }
      });
    }
    Creating a QueryTable using a query which joins two versioned tables in a geodatabase
    public async Task QueryTableJoinWithVersionedData()
    {
      await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
      {
        QueryDef queryDef = new QueryDef
        {
          Tables = "CommunityAddress JOIN MunicipalBoundary on CommunityAddress.Municipality = MunicipalBoundary.Name",
          SubFields = "CommunityAddress.OBJECTID, CommunityAddress.Shape, CommunityAddress.SITEADDID, CommunityAddress.ADDRNUM, CommunityAddress.FULLNAME, CommunityAddress.FULLADDR, CommunityAddress.MUNICIPALITY, MunicipalBoundary.Name, MunicipalBoundary.MUNITYP, MunicipalBoundary.LOCALFIPS",
        };
    
        using (Geodatabase testVersion1Geodatabase = new Geodatabase(new DatabaseConnectionProperties(EnterpriseDatabaseType.Oracle)
        {
          AuthenticationMode = AuthenticationMode.DBMS,
          Instance = "instance",
          User = "user",
          Password = "password",
          Database = "database",
          Version = "user.testVersion1"
        }))
        {
          QueryTableDescription queryTableDescription = new QueryTableDescription(queryDef)
          {
            Name = "CommunityAddrJounMunicipalBoundr",
            PrimaryKeys = testVersion1Geodatabase.GetSQLSyntax().QualifyColumnName("CommunityAddress", "OBJECTID")
          };
    
          // Will be based on testVersion1.
          using (Table queryTable = testVersion1Geodatabase.OpenQueryTable(queryTableDescription))
          {
            // Use queryTable.
          }
        }
    
        using (Geodatabase testVersion2Geodatabase = new Geodatabase(new DatabaseConnectionProperties(EnterpriseDatabaseType.Oracle)
        {
          AuthenticationMode = AuthenticationMode.DBMS,
          Instance = "instance",
          User = "user",
          Password = "password",
          Database = "database",
          Version = "user.testVersion2"
        }))
        {
          QueryTableDescription queryTableDescription = new QueryTableDescription(queryDef)
          {
            Name = "CommunityAddrJounMunicipalBoundr",
            PrimaryKeys = testVersion2Geodatabase.GetSQLSyntax().QualifyColumnName("CommunityAddress", "OBJECTID")
          };
    
          // Will be based on testVersion2.
          using (Table queryTable = testVersion2Geodatabase.OpenQueryTable(queryTableDescription))
          {
            // Use queryTable.
          }
        }
      });
    }
    Inheritance Hierarchy

    System.Object
       ArcGIS.Core.Data.QueryDef

    Requirements

    Target Platforms: Windows 11, Windows 10, Windows 8.1

    See Also