Select to view content in your preferred language

Referring to Attributes in a Query by Index number rather than name?

1288
2
09-17-2010 07:57 AM
MatthewCarey
Regular Contributor
I'm doing a fairly standard query which then takes the first returned record and uses it to populate a DataGrid. I would like to be able to exclude one or more of the attribute fields that are returned from appearing in the DataGrid (in this case the first attribute).

I know this can be easily done by specifying the fields by name when setting up the query like this:

query.OutFields.AddRange(new string[] {"COMPANY_ID","COMPANY","AREA_SQ_MI","PERIM_MI","SOURCE","UPDATE_","NOTES","COMP_FULL" });


However, I don't like hard-coding the field names in case the data set changes; I'd much rather just say 'exclude the field with the index value of 0.' I can see that there's the possibility of doing 'query.Outfields.Remove' or 'query.Outfields.RemoveRange' but can't get beyond that.
I'm really tripping up on the syntax for this and I'm hoping it can be done fairly simply.


Another option seems to be sticking with:

query.OutFields.Add("*");


And then excluding that field once the query results are returned. Here's a condensed version of what I do with the results:

 private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
        {
             FeatureSet featureSet = args.FeatureSet;
             
             Graphic selectedFeature = featureSet.Features[0];

             QueryDetailsDataGrid.ItemsSource = selectedFeature.Attributes;
         }


Presumably there's a way of excluding the first attribute that is returned without using its name, but again I'm tripping up on how to achieve it.

Any suggestions appreciated!

Matt Carey
0 Kudos
2 Replies
AliMirzabeigi
Emerging Contributor
Matt,

The OutFields property only accepts either IEnumerable of field names or just * for all fields. However; there is a way of excluding certain fields after a QueryTask has been executed:
The query should use * as its OutFields. In ExecuteCompleted event handler of your QueryTask loop through all Graphic objects in Features of the returned FeatureSet and remove the attribute in desired index before adding them to your GraphicsLayer. Here is a code snippet:
private void query_ExecuteCompleted(object sender, QueryEventArgs e)
{
   FeatureSet featureSet = e.FeatureSet;
   if (featureSet == null)
      return;
   GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
   foreach (Graphic graphic in featureSet.Features)
   {
      graphic.Attributes.Remove(graphic.Attributes.ElementAt(0));
      graphicsLayer.Graphics.Add(graphic);
   }
}


Hope this helps.
0 Kudos
MatthewCarey
Regular Contributor
Ali,

thanks for the response. Maybe it's my mistake but this line brought up some errors for me:

graphic.Attributes.Remove(graphic.Attributes.ElementAt(0));


The errors are:

Error 1 The best overloaded method match for 'System.Collections.Generic.Dictionary<string,object>.Remove(string)' has some invalid arguments

Error 2 Argument '1': cannot convert from 'System.Collections.Generic.KeyValuePair<string,object>' to 'string'


Any insight you have would be great, although please don't spend too much time on it as I realized that I could hard-code the field to remove rather than the ones to keep without it causing me too much hardship like this:

graphic.Attributes.Remove("OBJECTID");



Thanks again!
0 Kudos