Select to view content in your preferred language

Preventing Duplicate Graphics in GraphicsLayer

698
2
12-28-2010 12:25 PM
JoeHamilton
New Contributor
Hi all...

When iterating query results, is there an easy way to detect if the feature already exists in the current graphicsLayer?

For Each feature As Graphic In e.FeatureSet.Features

                Dim graph As Graphic = feature
                Dim myAddedGraphic = (From a In graphicsLayer.Graphics Select a Where a.Equals(graph)).First()

                    If myAddedGraphic Is Nothing Then
                        graphicsLayer.Graphics.Insert(0, feature)
                    End If
                Next  


This isn't working. I tried Where a IS graph, where a.Geometery.Equals(graph.Geometry) etc..

I just can't seem to use LINQ to find the item if it exists in the list. Any else get this working?
0 Kudos
2 Replies
AliMirzabeigi
Emerging Contributor
You cannot check existance of a feature on a layer against instances of the features returned by the query task, instead you should check for equality in their unique identifier attribute. Assuming you have an "ObjectID" field in your layer a sample C# code would be:

var graphic = from g in graphicsLayer.Graphics
                     where g.Attributes["ObjectID"].Equals(feature.Attributes["ObjectID"])
                     select g;
if (graphic.Count() == 0)
{
    // Code for adding the graphic here
}
0 Kudos
JoeHamilton
New Contributor
ali5110
Thanks for the info. I'm going to have to add an attribute to the graphic that is the name of the layer I'm querying. Then I can check for uniqueness across table & objectID.

That should work great! Thanks.
0 Kudos