tags:

views:

100

answers:

2

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.

A: 

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.

Telos
I'm not having trouble determining if an obj is an IEntitySoftDeletable, I'm having trouble casting an IQueryable<T> to an IQueryable<IEntitySoftDeletable>.The only way I can do it is to constrain "T" into being an IEntitySoftDeletable at the class or method level.
A: 

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
J.13.L
Same problem. It says that it can't cast "T" to "IEntitySoftDeletable".I have a class that accepts "T", where T has no contstraints.So when I get a queryable of type "T" it is not an IEntityQueryable, but it "might" be. What I want to do is check at runtime if it is, and if so, check the value of IsDeleted.Your method allows me to check IsDeleted, but the queryable can't be cast from type "T" to "IEntitySoftDeletable".I think I'm screwed and basically have to put a constraint on an overload.
Oh wait, sorry, I had a brain fart. Your method will work. I just can't return it into the same query variable "duh". I have to declare a new query.