Select to view content in your preferred language

How can I use rowCursor more efficiently?

472
1
08-16-2023 05:51 PM
tzz_12
by
New Contributor III

I'm creating a tool that would query a dataset and create a query table using a unique ID, then perform some calculations using the query table. Final, it would populate the calculated outputs in an attribute table of a new output feature class. The tools works, but I was wondering if there is a more efficient way of calculating the values and populating the output table. I would like to remove the repetitive method commented below as a separate method, but the problem I'm having is how to find the fields value without having it inside this row cursor loop? Is there a way I can get the field values (e.g. var rArea = row["Shape_Area"]) as the input parameter of the new method? Thanks for your help! Please see a snippet of my code below: 

 

public void AggregateMethod()
        {
            QueuedTask.Run(() =>
            {
                FeatureClassDefinition IntersectSortDef = IntersectSort.GetDefinition();
                QueryFilter qF = new QueryFilter { WhereClause = $@"{FieldName} = {UniqueID}" };

                using (var rowCursor = IntersectSort.Search(qF, false))
                {
                    while (rowCursor.MoveNext())
                    {
                        using (Row row = rowCursor.Current)
                        {    
                            var rArea = row["Shape_Area"];
                           //Performing the repetitive calculations here
                           //Ideally, I would like to take this middle part 
                           //and create a separate method with the repetitive 
                            //  calculations.
                      
                        }
                    }
                }
            });

           //return output values 

        }

 

0 Kudos
1 Reply
GKmieliauskas
Esri Regular Contributor

Hi,

If I understand you right, you want to call function from commented area.

So you could do it using Action or Func delegate. Below is sample with Action delegate:

    static void MyFunction(double param1)
    {
        Console.WriteLine($"Parameter: {param1}");
    }

    // Add an Action delegate as a parameter
public void AggregateMethod(Action<double> action)
        {
            QueuedTask.Run(() =>
            {
                FeatureClassDefinition IntersectSortDef = IntersectSort.GetDefinition();
                QueryFilter qF = new QueryFilter { WhereClause = $@"{FieldName} = {UniqueID}" };

                using (var rowCursor = IntersectSort.Search(qF, false))
                {
                    while (rowCursor.MoveNext())
                    {
                        using (Row row = rowCursor.Current)
                        {    
                            var rArea = row["Shape_Area"];
                           // Invoke the passed action
                           action(rArea);
                        }
                    }
                }
            });
        }


        // Usage example
        AggregateMethod(MyFunction);

If you want to return value from function use Func delegate:

    static bool MyFunction(double param1)
    {
        Console.WriteLine($"Parameter: {param1}");
        return param1 > 0;
    }

    // Add an Action delegate as a parameter
public void AggregateMethod(Func<double, bool> func)
        {
            QueuedTask.Run(() =>
            {
                FeatureClassDefinition IntersectSortDef = IntersectSort.GetDefinition();
                QueryFilter qF = new QueryFilter { WhereClause = $@"{FieldName} = {UniqueID}" };

                using (var rowCursor = IntersectSort.Search(qF, false))
                {
                    while (rowCursor.MoveNext())
                    {
                        using (Row row = rowCursor.Current)
                        {    
                            var rArea = row["Shape_Area"];
                           // Invoke the passed action
                           bool retVal = func(rArea);
                        }
                    }
                }
            });
        }


        // Usage example
        AggregateMethod(MyFunction);