Efficiently selecting X number of records in a layer

Discussion created by cpicciri on Feb 28, 2011
Latest reply on Mar 2, 2011 by cpicciri
Hi everyone,

I am creating a menu for loading data that has several tabs where each tab has different info about the selected layer, layer name, metadata, etc. On one of these tabs, I have a ListView where I want to show a sampling of records in table form, like ArcCatalog does. Neil last week showed me how to exclude the ESRI generated fields (OID, etc) in the sampling but now I have a new problem. For the sake of performance, I want to select only 20 records to display in the ListView or less if the layer has fewer than 20 records.

My first attempt was to select the feature class the user selected and put it into a cursor with no query filter so all features are selected. Then, cycle through the cursor until I have 20 features selected. This works fine on a layer with few features but on a layer with almost 500,000 features, getting the features into the cursor takes two or three minutes. My boss wants a response time of 20 seconds or less so this is totally unacceptable.

My second attempt was to generate a random number from 0 to the feature count for the layer, search for a feature that has that OID and if one found, add it to an array. Then create a selection set from the array and displaying the records in the ListView. This works very quickly and works great on most layers. However, I ran into one case where there were 287 features in the layer but the lowest OID number was 288. So my code got stuck in an infinte loop because the random number generated was always less than the number of features.

Does anyone have an idea how I can get around this? Been fighting with this since Thursday and I'm out of ideas!