Select to view content in your preferred language

Evaluate a QueryDef with joined tables or featureClass from a Feature Layer URL

633
2
Jump to solution
12-18-2023 11:53 AM
RejeanLabbe
New Contributor III
I have an issue when I use the Evaluate method with a QueryDef with joined tables or featureClass from a Feature Layer.
 
Everything works great when I use this code with a GDB or an SDE connection, but it doesn't work with a Feature Layer from ArcGIS Enterprise.
 

 

 

https://myserver.com/server/rest/services/myweblayer/FeatureServer

 

 

 
The connection to this Feature Layer works when I use a simple QueryFilter.
 
Here is a simplified version of my code :
 

 

 

 

using (Table oTable1 = oGeodatabase.OpenDataset<Table>(oTableDefinition.GetName()))
using (FeatureClass oFeatureClass = oGeodatabase.OpenDataset<FeatureClass>(oFeatureClassDefinition.GetName()))
{
	sTables = string.Format("{0} LEFT JOIN {1} ON {1}.MY_KEY = {0}.MY_KEY", oTable1.GetName(), oFeatureClass2.GetName());

	oQueryDef = new QueryDef
	{
		Tables = sTables,
		SubFields = sSubFields,
		WhereClause = sWhere,
	};

	using (RowCursor oRowCursor = oGeodatabase.Evaluate(oQueryDef, false))
	{
	}
}
 
My Feature Layer is on ArcGIS Enterprise but it might be the same for ArcGIS Online.
0 Kudos
1 Solution

Accepted Solutions
RejeanLabbe
New Contributor III

Thanks @Aashis.

Amr from Esri Canada told me about the Join class from the ArcGIS.Core.Data namespace and that did the trick.

using (Geodatabase sourceGeodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri("Path \\ to \\Geodatabase \\ one"))))
    using (Geodatabase destinationGeodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri("Path \\ to \\Geodatabase \\ two"))))
    using (Table sourceTable = sourceGeodatabase.OpenDataset<Table>("State"))
    using (Table destinationTable = destinationGeodatabase.OpenDataset<Table>("Cities"))
    {
      Field primaryKeyField = sourceTable.GetDefinition().GetFields().FirstOrDefault(field => field.Name.Equals("State.State_Abbreviation"));
      Field foreignKeyField = destinationTable.GetDefinition().GetFields().FirstOrDefault(field => field.Name.Equals("Cities.State"));

      VirtualRelationshipClassDescription virtualRelationshipClassDescription = new VirtualRelationshipClassDescription(primaryKeyField, foreignKeyField, RelationshipCardinality.OneToMany);

      using (RelationshipClass relationshipClass = sourceTable.RelateTo(destinationTable, virtualRelationshipClassDescription))
      {
        JoinDescription joinDescription = new JoinDescription(relationshipClass)
        {
          JoinDirection = JoinDirection.Forward,
          JoinType = JoinType.InnerJoin,
          TargetFields = sourceTable.GetDefinition().GetFields()
        };

        using (Join join = new Join(joinDescription))
        {
          Table joinedTable = join.GetJoinedTable();

          //Process the joined table. For example ..
          using (RowCursor cursor = joinedTable.Search())
          {
            while (cursor.MoveNext())
            {
              using (Row row = cursor.Current)
              {
                // Use Row
              }
            }
          }
        }
      }
    }

View solution in original post

0 Kudos
2 Replies
Aashis
by Esri Contributor
Esri Contributor

QueryDef is not supported in a feature service datastore.

0 Kudos
RejeanLabbe
New Contributor III

Thanks @Aashis.

Amr from Esri Canada told me about the Join class from the ArcGIS.Core.Data namespace and that did the trick.

using (Geodatabase sourceGeodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri("Path \\ to \\Geodatabase \\ one"))))
    using (Geodatabase destinationGeodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri("Path \\ to \\Geodatabase \\ two"))))
    using (Table sourceTable = sourceGeodatabase.OpenDataset<Table>("State"))
    using (Table destinationTable = destinationGeodatabase.OpenDataset<Table>("Cities"))
    {
      Field primaryKeyField = sourceTable.GetDefinition().GetFields().FirstOrDefault(field => field.Name.Equals("State.State_Abbreviation"));
      Field foreignKeyField = destinationTable.GetDefinition().GetFields().FirstOrDefault(field => field.Name.Equals("Cities.State"));

      VirtualRelationshipClassDescription virtualRelationshipClassDescription = new VirtualRelationshipClassDescription(primaryKeyField, foreignKeyField, RelationshipCardinality.OneToMany);

      using (RelationshipClass relationshipClass = sourceTable.RelateTo(destinationTable, virtualRelationshipClassDescription))
      {
        JoinDescription joinDescription = new JoinDescription(relationshipClass)
        {
          JoinDirection = JoinDirection.Forward,
          JoinType = JoinType.InnerJoin,
          TargetFields = sourceTable.GetDefinition().GetFields()
        };

        using (Join join = new Join(joinDescription))
        {
          Table joinedTable = join.GetJoinedTable();

          //Process the joined table. For example ..
          using (RowCursor cursor = joinedTable.Search())
          {
            while (cursor.MoveNext())
            {
              using (Row row = cursor.Current)
              {
                // Use Row
              }
            }
          }
        }
      }
    }
0 Kudos