0

I got the following objects:

class Base { }
class A : Base {}
class B : Base {}
class C : Base {}

enum BaseType
{
   A,
   B,
   C,
}

ILookup<BaseType, Base> lookup = GetBaseInformation();

The GetBaseInformation method returns me a list of Base objects grouped by base type. With that I would like to set a separated list of properties with each of the BaseTypes and I'm trying to do it like this:

IReadOnlyCollection<A> aTypes = lookup[BaseType.A] as IReadOnlyCollection<A>;
// returns null even if IGrouping<BaseType, A> is there

IReadOnlyCollection<A> aTypes = (IReadOnlyCollection<A>) lookup[BaseType.A];
> Cannot cast 'lookup[BaseType.A]' (which has an actual type 
> of 'System.Linq.Lookup<BaseType,Base>.Grouping') 
> to 'System.Collections.Generic.IReadOnlyCollection<A>'

IReadOnlyCollection<A> aTypes = lookup[BaseType.A].ToList().AsReadOnly()
// returns the list of objects

What am I doing wrong? What is the best way to do this?

Hélder Gonçalves
  • 3,092
  • 12
  • 33
  • 63

0 Answers0