Reputation: 4416
I have a Platform class as a base class and there are 2 more classes IOSPlatform and AndroidPlatform that are derived from Platform.
In addition to this, there is a Device class that has Platform object as a navigation property.
While querying all IOS platform devices, the linq expression below is working like a charm.
devices.Where(t=> t.Platform is IOSPlatform)
I want to improve this query with making it dynamic check of class type such as:
Platform p = new IOSPlatform();
devices.Where(t=> t.Platform is /*derived class of p object*/)
Is there a way to do this ?
Best Regards,
Kemal
Upvotes: 0
Views: 1195
Reputation: 62002
I'm not sure I understand, but it sounds like you want
devices.Where(t =>
p.GetType().IsAssignableFrom(t.Platform.GetType())
);
Upvotes: 1
Reputation: 116168
Platform p = new IOSPlatform();
devices.Where(t=> t.Platform.GetType()==p.GetType())
Upvotes: 4
Reputation: 16623
What about:
Platform p = new IOSPlatform();
devices.Where(t=> t.Platform.GetType().BaseType == p.GetType());
Upvotes: 1
Reputation: 1008
Are you trying to verify if the object on your list inherits from Platform? The "is" operator already verifies that .. you could just use this:
devices.Where(d => d.Platform is Platform);
Or even use the OfType method:
devices.OfType<IOSPlatform>();
Upvotes: 0