C# Query From GeoDatabase to Return Single Value (Arc Objects)

Discussion created by Jamari_L_Powers on Apr 26, 2014
Latest reply on Apr 28, 2014 by Jamari_L_Powers
Using C#, arcobjects and IQueryDef, I've been learning how to write queries to return rowsets and multiple results. I would also use ICursor and Irow to loope through each record from the cursor. This is great for multiple results but I am having trouble returning one result.

I want to run a query to return a single integer from a query. Would I need to have ICursor or IRow still? I've been looking online for examples but everything incorporates a ICursor / IRow.

I was using something along these lines:

private static List<object> GenericQuery(string sQueryColumn, string sFrom, string sWhere)

// connect to workspace

List<object> myObjects = new List<object>();

IQueryDef pQD = pFeatureWorkspace.CreateQueryDef();
pQD.Tables = sFrom;
pQD.SubFields = sQueryColumn;
pQD.WhereClause = sWhere;

ICursor pCursor = pQD.Evaluate();

//Loop through each of the records in the cursor                        
            IRow pRow = pCursor.NextRow();
            while (pRow != null)
                object myObject = myObject = pRow.get_Value(lIndex);
                //Increment the cursor
                pRow = pCursor.NextRow();

Of course, there would be an actual table name, subfields, and a where clause there. I would use ICursor to evaluate my query def but I just want one result. So if I created a function that returned an integer with a query that just returned an integer, would I still need ICursor / IRow?

Not sure how to go about this. Not too many examples online. Any feedback on this would be greatly appreciated. Thanks in advance.