Getting the FIDs for a selection.

Aug 9, 2012 at 8:47 AM

I'm using the following code:

                var ls1 = CeLayer.Selection.ToFeatureList();
                
                var SelectedItems = new List<int>(); 
                foreach (var dg in ls1) 
                {

                    SelectedItems.Add(dg.Fid); 
                }
                listBox2.Items.Add(string.Join(",",SelectedItems));
                CeLayer.UnSelectAll();
                CeLayer.Select(SelectedItems);

If I use this on a selection in a polygon, the selection changes: it seems that the Feature Id's that are expected by imapfeaturelayer.select are different then the ones I get from imapfeaturelayer.selection.tofeaturelist.

 

What am I doing wrong? How do I get the right Id's to use with select?

Aug 9, 2012 at 8:56 AM

Ok, after debugging I noticed that all ID's were shifted one number. The following code works:

                var ls1 = CeLayer.Selection.ToFeatureList();
                
                var SelectedItems = new List<int>(); 
                foreach (var dg in ls1) 
                {

                    SelectedItems.Add(dg.Fid-1); 
                }
                listBox2.Items.Add(string.Join(",",SelectedItems));
                CeLayer.UnSelectAll();
                CeLayer.Select(SelectedItems);

Developer
Aug 9, 2012 at 5:25 PM

I notice the docs on the Select() say that it takes The zero-based integer index of the feature. I'm not sure that Fid represents this index. I think you just got lucky because the Fid is close to the index. I would expect if you create and remove features, the Fid and index will get farther apart.

It may be more appropriate to use another overload of the Select method, which takes a reference to the feature.