开发者

c# Union Operator

Scenario

I have three IEnumerable开发者_如何学编程 lists - IEnumerable<Dog>, IEnumerable<Cat> and IEnumerable<Horse>.

I want them in all in a single IEnumerable<NameColor>.

I have a public static explicit operator NameColor defined on each of the Dog, Cat and Horse objects to allow me to cast them to NameColor

So:

IEnumerable<NameColor> list = dogs as IEnumerable<NameColor>;
list = list.Union(cats as IEnumerable<NameColor>);
list = list.Union(horses as IEnumerable<NameColor>);

However, this doesnt work. I get the error Value cannot be null. Parameter name: first but I dont see a parameter called first even being an option?!


first is the implicit "this" parameter of Enumerable.Union. It's throwing an exception because the as operator's result is null.

Basically the problem is that having a conversion operator for Dog doesn't mean that you can treat an IEnumerable<Dog> as an IEnumerable<NameColor>. Use Select instead, basically.

There are actually two reasons behind your conversion not working:

  • as doesn't apply conversions anyway, so if you do:

    NameColor nameColor = dog as NameColor;
    

    that would still leave nameColor as null.

  • Just because there's a conversion between element types doesn't mean that conversion is applied to the collection types themselves. Generic variance only works for reference type conversions, which are representation-preserving.


I suggest:

var list = dogs.Select(d => (NameColor)d)
               .Union(cats.Select(c => (NameColor)c))
               .Union(horses.Select(h => (NameColor)h));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜