This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hi, Is it possible to query for an object (that is part of an inheritance heirarchy) given the required object Type. From memory, I believe you can use the is operator, but this would require a type parameter, rather than a Type instance. For example, I need to be able to do something similar to the following: public IEnumerable<TFoo> GetResults() {
Type featureType = DeriveTypeFromFoo(typeof(TFoo));
var results = uow.Features.Where(f => f.GetType().Equals(featureType));
return (IEnumerable<TFoo>)results;
}
I'm trying to do this at runtime (ie avoiding using a generic type parameter which would need to be defined at compile time) - featureType is being determined using reflection. Any suggestions? Cheers, |
|
|
This isn't possible as written. Here are some possible options: * Use Reflection to get the discriminator of the chosen featureType (via DiscriminatorAttribute), and write a query that filters on that discriminator: Where(f => f.FeatureKind == theKind). * Create an is expression using the expressions API instead of C# lambda syntax: var p = Expression.Parameter(typeof(Feature), "f"); Incidentally note the semantics are slightly different: an is expression accepts derived types, an equality expression accepts only the exact type. |
|
|
Brilliant - thanks Ivan - I've gone with the dynamic expression tree, and it works great.
Cheers,
|
|