1
votes

Given this:

var query = context.GetTable<T>();

Where "T" is a generic entity passed into the method,

I'd like to do something like this:

if(typeof(TEntity) is IEntitySoftDeletable)
  query = query.Cast<IEntitySoftDeletable>.Where(ent => !ent.IsDeleted);
}

Is this possible?

Currently it's telling me that I can't cast this way.

I realize that I could force parameter "T" to be an IEntitySoftDeletable at the class or method level, but I'm trying to avoid that and provide more flexibility from the same method.

2
Does T implement IEntitySoftDeletable ? - bytebender
not always...T could be any number of types, some of which implement IEntitySoftDeletable. What I'd like is to append a predicate to the queryable if T is an IEntitySoftDeletable. What I'm trying to avoid is requiring a overload for this seperate behaviour. - Scott

2 Answers

0
votes

Not sure I complete understand what you want but would this work?

query.OfType<IEntitySoftDeletable>().Where(ent => !ent.IsDeleted); //OfType will only give the ones that are IEntitySoftDelteable
0
votes

You can test for an interface using

obj.GetType().GetInterface("IEntitySoftDelete") == null

I think that should work for what you need... just make sure to add a using for System.Reflection.