Question

I'm using big api sdk on a winform application ( Winform and no WPF ! ).

I'm able to add PushPin. I would like to delete somes pushpin.

I saw some linq method but i'm not able to compile because the "First" is not found. :

var pushpin = MyMapUserControl.MyMap.Children.First(p => (p.GetType() == typeof(Pushpin) && ((Pushpin)p).Tag == "FindMeLater"));
MyMapUserControl.MyMap.Children.Remove(pushpin);

My error :

'System.Windows.Controls.UIElementCollection' doesn't contain a definition for 'First' and no extensions method 'First' for a first argument 'System.Windows.Controls.UIElementCollection' has been found.

I add the include :

using System.Linq;

Anyone could help me please ?

Was it helpful?

Solution

System.Windows.Controls.UIElementCollection implements IEnumerable but not IEnumerable<UIElement>, so the Linq methods do not directly apply since they only work on IEnumerable<T> instances.

One way is to "extract" the PushPins as an IEnumerable<PushPin> using OfType:

var pushpin = MyMapUserControl.MyMap
                              .Children
                              .OfType<Pushpin>()
                              .First(p => p.Tag == "FindMeLater");

OTHER TIPS

Use Enumerable.OfType

var pushPin = MyMapUserControl
                 .MyMap
                 .Children
                 .OfType<PushPin>()
                 .FirstOrDefault(p=> p.Tag == "FindMeLater");
if(pushPin != null)
   MyMapUserControl.MyMap.Children.Remove(pushpin);

System.Windows.Controls.UIElementCollection doesn't implement IEnumerable<T>, thus LINQ will not be available. You may see this question Does LINQ work with IEnumerable?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top