AnsweredAssumed Answered

Quickest way to get Row from OID

Question asked by mjk5000 on Sep 7, 2018
Latest reply on Sep 7, 2018 by wkaiser-esristaff

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;
            }
        }

Outcomes