Offler
Offler

Reputation: 1223

Using a type in linq

I have a large number of classes. From this list I get a specified type

Type aType = Type.GetType(...);

Now I want to use this type in a linq statement like:

var aResult = from obj in scope.Extent<aType>() select obj;

This does not seem to be possible, as Extent does not accept Type.

Is there any way now (with .net 4.5) to call the statement?

All I want to do is to say use Type as class. Don't invoke the class, only get with linq all objects of this type from a scope.

Upvotes: 0

Views: 129

Answers (1)

Rawling
Rawling

Reputation: 50114

You can't use an instance of Type as a generic type argument - not at compile time, anyway.

If you want objects of a specific type, you could do

var aResult = from obj in scope where obj.GetType() == aType select obj;

Note that this requires an exact type match, rather than any kind of "can be assigned to" relationship.

Also note that this will only get you a series of objects - again there's no compile-time way to cast things to an instance of Type.

Upvotes: 2

Related Questions