-1

I have a collection (IQueryable<object>/IEnumerable<object>) and a given Type that I get by reflection at runtime. How can I cast the IQueryable<object> to the specific Type when:

  1. The Type is a class e.g. Person (and I want to make a cast of the IQueryable<object> to IQueryable<Person>)
  2. The Type is a generic collection e.g. IQueryable<Person>

Note that Type is a variable that I get through aVariable.GetType(). I do not know the type beforehand, therefore I cannot use Cast<type> nor (type).

Midas
  • 564
  • 6
  • 21

1 Answers1

1

If targeted type is known, go with this

yourIEnumerable.Select(x => (YourType) x);

But as you noted, you don't know the type of the targeted type and you are getting it using object.GetType() so I would recommend to follow this link

Mohammad Ali
  • 551
  • 7
  • 17