Select to view content in your preferred language

Collection View Source

3059
19
02-04-2011 11:31 AM
MeConfused
Emerging Contributor
I am trying to Sort the results from a buffer using Collection View Source...I think I am on the right path here but cant seem to get the sort to perform or refresh the listbox.
In this case I am trying to sort by the Buffer Distance being created fro each selected feature found inside the buffer
Note: BUFFER_DISTANCE is not a field in the feature...its being created or set below in blue

Any quick thoughts?

    Private Sub QueryTask_ExecuteCompletedBuffer2(ByVal sender As Object, ByVal args As QueryEventArgs)

      ' Set up the collection View Source in order to Sort the results of the query by DISTANCE       
                Dim cs As New CollectionViewSource
                cs.SortDescriptions.Add(New SortDescription("BUFFER_DISTANCE", ListSortDirection.Ascending)) 
                cs.Source = args.FeatureSet.Features        
     ' Apply the sort to the listbox       
                imageListBuffer.ItemsSource = cs.View        
     ' refresh the listbox        
                cs.View.Refresh()

        Dim featureSet As FeatureSet = args.FeatureSet
        imageListBuffer.ItemsSource = args.FeatureSet.Features

        ' Set the Buffer Variable for the Center Point of the buffer to a MAPPOINT not String to the variable being set 
        ' in the GeometryService_BufferCompleted2...the Variable is globally define at the to of the code page
        Dim _XYLocationBuffer As MapPoint = Nothing
        _XYLocationBuffer = _XYLocation

        If args.FeatureSet.Features.Count < 1 Then
            MessageBox.Show("No features found")
            Return
        End If
        For Each selectedGraphic As Graphic In args.FeatureSet.Features
            selectedGraphic.Symbol = TryCast(LayoutRoot.Resources("DefaultMarkerSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol)
            _resultsGraphicsLayer.Graphics.Add(selectedGraphic)

            ' Get the center point of each selected graphic found in the buffer
            Dim c As ESRI.ArcGIS.Client.Geometry.MapPoint = selectedGraphic.Geometry.Extent.GetCenter()
            ' Call the GetDistance Function...Set the Distance to the BUFFER_DISTANCE variable for each graphic, for display in the listbox            
             selectedGraphic.Attributes("BUFFER_DISTANCE") = GetDistance(_XYLocationBuffer, c)
       
 Next selectedGraphic

    End Sub
0 Kudos
19 Replies
MeConfused
Emerging Contributor
For me, I think Linq query might be the easiest solution. Once you have calculated and added the Distance, you can order by this attribute. You can then set ListBox ItemSource with the sortedGraphics.

var sortedGraphics = from g in graphicsLayer.Graphics
   orderby (double)g.Attributes["Distance"] ascending
   select g;


Thats in C# right?  I was trying to convert it....thats from the example I posted right....
I know I am very close here...thanks to you
This is my feable attempt to convert that C# code....

        Dim SortedList As FeatureSet = args.FeatureSet.OrderBy([Name],String)

Then set the ListBox
        imageListBuffer.ItemsSource = SortedList
So LIKE this?  I am so confused I dont know if its done before the IMageListBuffer Listbox gets binded or if its in the FOR EACH loop....

Would it go in the FOR EACH loop or above where the binding to the listbox occurs???
0 Kudos
MeConfused
Emerging Contributor
I AM THINKING that the dimented code I have above to do the sort should be right before the LIstBox binding....
THAT RIGHT?

Like THis

        Dim featureSet As FeatureSet = args.FeatureSet
        Dim SortedGraphics As Graphic = args.FeatureSet.OrderBy(selectedGraphic.Attributes([NAME], String)
        imageListBuffer.ItemsSource = SortedGraphics

THIS has to be close....ugggggg
??     Dim SortedGraphics As Graphic = args.FeatureSet.OrderBy("Name", ListSortDirection.Ascending)
0 Kudos
JenniferNery
Esri Regular Contributor
I'm not sure how correct the syntax is, I used http://www.developerfusion.com/tools/convert/csharp-to-vb/

Since you still need to calculate the distance of each graphic from the buffer point, you should do the following code after you have added this attribute for each graphic.
Dim sortedGraphics = From g In args.FeatureSet.Features Order By CDbl(g.Attributes("Distance")) Ascending
imageListBuffer.ItemsSource = sortedGraphics 
0 Kudos
JayKappy
Frequent Contributor
Thank you once again....I knew I was getting close...things are starting to become more familar...And YES this was much easier than the CollectionViewSource...although I still may try that just to learn...

Questions: Which I hope you can shed some light on...

What does the 'g' represent? I just cant grasp that...I understand the args.FeatureSet.Features which comprise the results of the query...but the 'g' is confusing me...

Either or below and my data is returned to the listbox descending...thoughts?
For soem reason cant get it to ascend the order?

Thank you, your help is very much appreciated...So I guess the title of this forumn entry has now lost its meaning as I converted to Linq ...

        Dim sortedGraphics = From g In args.FeatureSet.Features Order By CDbl(g.Attributes("Distance")) Ascending

        Dim sortedGraphics = From g In args.FeatureSet.Features Order By CDbl(g.Attributes("Distance")) Descending


If I move to a string field I can get the sort to work properly....seems to be on this Distance field....hmmmmm

This works....
Dim sortedGraphics = From g In args.FeatureSet.Features Order By (g.Attributes("NAME")) Ascending
0 Kudos
JayKappy
Frequent Contributor
...............................................
0 Kudos
JenniferNery
Esri Regular Contributor
You can look at Linq samples here: http://msdn.microsoft.com/en-us/vbasic/bb737912#obydescsimp1

'g' in the Linq query is a temporary variable that represents element in your collection. It serves the same purpose as in foreach(var g in args.FeatureSet.Features).

If you convert the attribute value to string, the sort for (1,2,11) will be (1,11,2) because they will be treated as strings.
0 Kudos
JayKappy
Frequent Contributor
Thanks for the link....

I think you may have misunderstood me....I wasnt saying that I converted the value to a string...I used a string attribute "NAME" (string) instead of "Distance" (double)

        'Dim sortedGraphics = From g In args.FeatureSet.Features Order By (g.Attributes("NAME")) Ascending
        'Dim sortedGraphics = From g In args.FeatureSet.Features Order By (g.Attributes("NAME")) Descending


Using the String Attribute "NAME" the Ascending and Decending worked fine.

If I use the Distance (double) it was AWALYS is descending not matter if I specified Ascending or Descending...

BOTH OF THESE sort descending...was trying to fugure out why the Double wont sort Ascending...even if I specify "Ascending" as seen below

        Dim sortedGraphics = From g In args.FeatureSet.Features Order By CDbl(g.Attributes("DISTANCE")) Ascending       
        Dim sortedGraphics = From g In args.FeatureSet.Features Order By CDbl(g.Attributes("DISTANCE")) Descending 
0 Kudos
JenniferNery
Esri Regular Contributor
I cannot replicate the same issue you are seeing with the following short sample:

private void GraphicsLayer_Initialized(object sender, EventArgs e)
{
 GraphicsLayer l = sender as GraphicsLayer;
 Graphic gr = new Graphic();
 gr.Attributes["Distance"] = 2.3;
 l.Graphics.Add(gr);

 gr = new Graphic();
 gr.Attributes["Distance"] = 4.0;
 l.Graphics.Add(gr);

 gr = new Graphic();
 gr.Attributes["Distance"] = 2.3;
 l.Graphics.Add(gr);

 gr = new Graphic();
 gr.Attributes["Distance"] = 3.2;
 l.Graphics.Add(gr);

 gr = new Graphic();
 gr.Attributes["Distance"] = 1.0;
 l.Graphics.Add(gr);

 var sortedGraphics = from g in l.Graphics
   orderby (double) g.Attributes["Distance"] ascending
   select g;
 myList.ItemsSource = sortedGraphics;
}


<ListBox x:Name="myList" VerticalAlignment="Top" HorizontalAlignment="Center" >
 <ListBox.ItemTemplate>
  <DataTemplate>
   <TextBlock Text="{Binding Attributes[Distance]}"/>
  </DataTemplate>
 </ListBox.ItemTemplate>
</ListBox>


Maybe the C# to VB conversion was wrong?
0 Kudos
JayKappy
Frequent Contributor
Think I found it....I had the Linq before the code that added the graphics and called the Distance function to calculate it.

I moved it below and bam...it worked....THANK YOU VERY MUCH FOR YOUR HELP...sooooo very appreciated...
I remembered you saying to do the sort after I calculated the distances.....THANK you again

        Dim featureSet As FeatureSet = args.FeatureSet

' CODE WAS HERE        

        Dim _XYLocationBuffer As MapPoint = Nothing
        _XYLocationBuffer = _XYLocation

        If args.FeatureSet.Features.Count < 1 Then
            MessageBox.Show("No features found")
            Return
            End If
        For Each selectedGraphic As Graphic In args.FeatureSet.Features
                'selectedGraphic.Symbol = TryCast(LayoutRoot.Resources("DefaultMarkerSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol)
            selectedGraphic.Symbol = TryCast(LayoutRoot.Resources("StrobeMarkerSymbol_Blue"), ESRI.ArcGIS.Client.Symbols.Symbol)
            _resultsGraphicsLayer.Graphics.Add(selectedGraphic)
                'graphicsLayer.Graphics.Add(selectedGraphic)

                ' Get the center point of each selected graphic found in the buffer
            Dim c As ESRI.ArcGIS.Client.Geometry.MapPoint = selectedGraphic.Geometry.Extent.GetCenter()
                ' Call the GetDistance Function...Set the Distance to the BUFFER_DISTANCE variable for each graphic, for display in the listbox
            selectedGraphic.Attributes("DISTANCE") = GetDistance(_XYLocationBuffer, c)

        Next selectedGraphic

' NOW HERE        
       Dim sortedGraphics = From g In args.FeatureSet.Features Order By CDbl(g.Attributes("DISTANCE")) Ascending
        imageListBuffer.ItemsSource = sortedGraphics
0 Kudos
JayKappy
Frequent Contributor
Anyone have any questions please feel free to contact me...

MeConfused1982@yahoo.com

Thanks again Jennifer
0 Kudos