Attribute Query Distinct values

4501
15
08-03-2011 08:44 AM
gabrielvazquez
New Contributor
I am running a simple Attribute query, nearly identical to the below link.
http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#AttributeQuery

However, I running the query against an incident layer that uses a statefield. So we may have multiple incidents for each state and as a result the initial combobox dropdown may have multiple states. So if there are 10 incidents in Maryland, there are 10 marylands in the combo box. Which leads me to my question.

I am trying to see if anyone else has run into this issue, and has figured out a way to provide distinct results. So instead of seeing 10 marylands in the combobox, you see only one.

Again, not sure if this has to do with the
query.Outfields.AddRange(new string[] { "(STATENAME)" });
But if anyone has anyone recommendations on using Attribute QueryTask and definining distinct feature it would be much appreciated.
0 Kudos
15 Replies
JMcNeil
Occasional Contributor III
I think you want to run a distinct when populating your combobox

REPLACE THIS CODE:
   foreach (Graphic graphic in args.FeatureSet.Features)
                {
                    QueryComboBox.Items.Add(graphic.Attributes["STATE_NAME"].ToString());
                }


With This

  foreach (Graphic graphic in args.FeatureSet.Features)
                {
                    //Set Distinct
                    QueryComboBox.Items.Add = (from g in args.FeatureSet.Features
                                                   orderby g.Attributes["STATE_NAME"].ToString()
                                                   select g.Attributes["STATE_NAME"]).Distinct();
                   
         
                }

                 QueryComboBox.SelectedIndex = 0;
                return;



Not 100% sure of that...off the top of the head.
0 Kudos
ChristopherHill
Occasional Contributor
esrijay is right. you will need to use a linq query to filter out your redundant states and populate your combobox with unique entries.

thanks esrijay for your contribution.
0 Kudos
gabrielvazquez
New Contributor
esrijay is right. you will need to use a linq query to filter out your redundant states and populate your combobox with unique entries.
.


Thanks for the response guys. Shoulds esrijays example code work or are there other examples that can be provided demonstrating how to use a linq query for filtering purposes? I seem to be having issues with the below line of code in esrijays example.

select g.Attributes["TYPEDESC"]).Distinct();

The error is 'object does not containe a definition for 'Distinct' and no extension metion 'Distinct' accepting a first argment of type.
0 Kudos
JMcNeil
Occasional Contributor III
O..NO... sorry about that I was in the middle of writing something else and placed in my field name TYPEDESC  is the field and you are using STATE_NAME so replace STATE_NAME with the two TYPEDESC fields.  I updated the code above so you should be able to copy the new code and replace it.
0 Kudos
gabrielvazquez
New Contributor
I apologize, had a typo on the line, which I corrected. However, I am now having issues with QueryComboBox.Items.Add.
Error is simply saying Cannot assign to 'Add' because it is a method group. Trying to fix it now.
0 Kudos
JMcNeil
Occasional Contributor III
Umm.  Try adding the namespace (at the top of you code behind page (xaml.cs file).

using System.Linq;
0 Kudos
JMcNeil
Occasional Contributor III
You might also have to add the ref to your app. 
Right click Reference folder
Click add reference
Click the .Net Tab
Find System.Xml.Linq
Add. 
Hope that works.
0 Kudos
gabrielvazquez
New Contributor
You might also have to add the ref to your app. 
Right click Reference folder
Click add reference
Click the .Net Tab
Find System.Xml.Linq
Add. 
Hope that works.


Yes I actually went through and added this previously. The reference and the namespace.
0 Kudos
DarinaTchountcheva
Occasional Contributor II
Gabriel,

This should work:

var states = (from g in args.FeatureSet.Features
                   select g.Attributes["STATE_NAME"]).Distinct().OrderBy(s => s);
QueryComboBox.ItemsSource = states;


Simply remove the foreach loop, and Items.Add replace with ItemsSource.

Or you could do it this way too:

 string[] states = args.FeatureSet.Features.Select(s => s.Attributes["STATE_NAME"].ToString()).Distinct().OrderBy(t => t).ToArray();
 QueryComboBox.ItemsSource = states;


Here is an article on using Linq with Distinct and Order By:

http://programminglinq.com/blogs/marcorusso/archive/2008/07/20/use-of-distinct-and-orderby-in-linq.a...

Hope, it works!
0 Kudos