0

I have an implicit conversion method in one of my classes.

public static implicit operator MobileTag(DetailGroup dg)
{
    return new MobileTag { Id = dg.ID, Name = dg.Name, Answers = dg.Details };
}

This works just fine with a List if I foreach through it:

foreach (var tag in tagsThisEvent)
{
    ev.EventTags.Add(tag);   // lookin' good !!
}

But then if I use the generic Linq Cast method I get an InvalidCastException:

ev.EventTags = tagsThisEvent.Cast<MobileTag>().ToList(); // ftl !!

I can't F11 into it to see what it is balking on. Any suggestions?

When is it okay to use List.Cast().ToList() ??

micahhoover
  • 2,101
  • 8
  • 33
  • 53
  • What is the declaration of `tagsThisEvent`? Mouseover the `var` in the foreach statement. What type is it resolving to? – Sonny Childs Feb 02 '16 at 20:11
  • Possible duplicate http://stackoverflow.com/questions/14523530/why-does-the-linq-cast-helper-not-work-with-the-implicit-cast-operator – fhogberg Feb 02 '16 at 20:11

0 Answers0