Quickest way to get Row from OID

791
4
09-07-2018 12:03 PM
MKa
by
Occasional Contributor III

I am trying to move some of my code from inspector to Row for the purposes of saving in RowCreate and Change Events.  Is there a quick way to get a Row from an OID.  I think I am missing something.  For instance below, I want the first selected item from the selection set, but I get an OID and need to quickly get the ROW for it.  I know I can do the lookup and get the row cursor, but is there an easier way than this to get a Row for a OID than GetRowForOID like I have below?  Much like the Inspector does?

//I need to get what the user, if multiple, i need to get the first

public static Task<Row> GetFirstSelectedRow()
        {
            return QueuedTask.Run(() =>
            {
                try
                {
                    Row selectedRow = null;

                    // get the currently selected features in the map
                    var selectedFeatures = MapView.Active.Map.GetSelection();

                    if (selectedFeatures.Count > 0)
                    {
                        // get the first layer and its corresponding selected feature OIDs
                        var firstSelectionSet = selectedFeatures.First();

                        // load the selected features into the inspector using a list of object IDs
                        if (firstSelectionSet.Value.Count > 0)
                        {
                            selectedRow = GetRowForOID(firstSelectionSet.Key as FeatureLayer, firstSelectionSet.Value.First());
                        }
                    }
                    return selectedRow;
                }
                catch (Exception e)
                {
                    LogWriter.LogError("FeatureServiceManagement - getFirstSelectedItem", e);
                    return null;
                }
            });
        }

public static Row GetRowForOID(FeatureLayer inFeatureLayer, long inOID)
        {
            try
            {
                Row ReturnRow = null;
                if (inFeatureLayer == null)
                {
                    return null;
                }

                string OidFieldName = inFeatureLayer.GetTable().GetDefinition().GetObjectIDField();
                QueryFilter qf = new QueryFilter()
                {
                    WhereClause = string.Format("{0} = {1}", OidFieldName, inOID)
                };

                // apply the query filter to the feature layer in question
                RowCursor rowCursorSearch = inFeatureLayer.Search(qf);

                if (rowCursorSearch != null && rowCursorSearch.MoveNext())
                {
                    if (rowCursorSearch.Current != null)
                    {
                        ReturnRow = rowCursorSearch.Current;
                    }
                }

                return ReturnRow;

            }
            catch (Exception e)
            {
                LogWriter.LogError("FeatureServiceManagement - GetRowForOID", e);
                return null;
            }
        }
0 Kudos
4 Replies
CharlesMacleod
Esri Regular Contributor

I'd recommend calling Search on the Selection set like so:

var rowcursor = selection.Search();

or even:

var rowcursor = MapView.Active.Map.GetSelection().Search();

reference Selection.Search

0 Kudos
MKa
by
Occasional Contributor III

Did you extend the dictionary or something in order to use .search() on it?  I can't get it to work like you have above?

0 Kudos
CharlesMacleod
Esri Regular Contributor

yes, you're quite right. It's (Basic) FeatureLayer.GetSelection() that returns a Selection and not Map.GetSelection().

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

I usually use this snippet of code knowing that through a selection I get a dictionary with the MapMember as key and a list of object ids as the value for each MapMember.  I am using a single mapmember and the corresponding objectId list as input for my sample method below (I am using similar code but this snippet is only pseudo code):

    // this in only pseudo code
    // call from MCT
    public RowCursor GetRowCursor(MapMember mapMember, List<long> objectIds)
    {
      var basicFeatLayer = mapMember as BasicFeatureLayer;
      if (basicFeatLayer == null) return null;
      // Create a query filter to fetch the appropriate rows for this mapmember
      QueryFilter queryFilter = new QueryFilter()
      {
        ObjectIDs = objectIds
      };
      return basicFeatLayer.Search(queryFilter);
    }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos