Sorting a table using VB.Net

2694
3
08-04-2010 12:15 PM
DavidKelly1
New Contributor
Does anyone have any sample code that sorts an attribute table?  I am trying to sort an attribute table using a single field.  Below is the code I am using:

        Dim ptableSort As ITableSort
        Dim pQueryFilter As IQueryFilter

        pQueryFilter = New QueryFilter
        ptableSort = New TableSort

        With ptableSort
            .Fields = "stand"
            .Ascending("stand") = True
            .Table = pStdDisplaytable
         End With



         pStandCursor = pStdDisplayTable.SearchDisplayTable(Nothing, False)

         pstdfeature = pStandCursor.NextFeature

Thanks,

David
0 Kudos
3 Replies
ThavitinaiduGulivindala
Occasional Contributor
0 Kudos
JanDuske
New Contributor
You can trick the QueryFilter instead of using this clumsy tablesort mechanism. (OK, that's not really elegant either, but still better imho):

pQueryFilter.Subfields = "stand";
pQueryFilter.WhereClause = "[your selection criteria]) order by (stand";

The trick is the closing and opening bracket. the first (closing) bracket is the counterpart to an opening bracket created by the ArcObjects parser, which builds a multiversioned SQL query for your where-clause. It builds a huge expression from its managed tables and finally adds a "Where ([yourstring])" at the end. So when you directly use an order by statement, it is pushed into the generated where statement causing an error of course. But if you add the closing bracket after your where clause is complete and afterwards start an order by (or group by or whatever), the generated statement has correct syntax and processes as expected. Just make sure to have an opening bracket in front of the last order by parameter, as a counterpart of the automatically generated closing bracket.

The generated SQL qery for the two code lines above would be like

Select [a huge ammount of columns] from ([an even bigger ammount of joins of uncounted tables]) v_1234 where ([your selection criteria]) order by (stand)

(Your whereclause string red, the automatically added brackets from the ArcObjects parser green)

It works in some combinations for some other methods too, like IQueryDef's, and it does not for FeatureWorkspace.OpenFeatureQuery sadly. Just try it 😉
0 Kudos
vincentLahaye
New Contributor II
you can't use "Order by" for a Iqueryfilter.whereclause.  you need to use :

IQueryFilterDefinition itfOrderBy = itfYourFilter as IQueryFilterDefinition;
itfOrderBy.PostfixClause = "ORDER BY " + YourField

Vincent
0 Kudos