I have several entity framework classes that implement the following an IInactive interface.
public interface IInactive
{
bool inactive { get; set; }
}
For example, my Order class is defined as follows:
public class Order : IInactive
{
[Key]
[Required]
public Guid orderId { get; set; }
...
public bool inactive { get; set; }
}
I am trying to implement a generic method that can be used against all objects (entities) whether they implement the IInactive interface or not. It would be called as follows:
var query = GetAllActive<Order>();
My code for this generic method looks like this:
public IQueryable<T> GetAllActive<T>() where T : class
{
DbSet<T> dbSet = this._db.Set<T>();
// Does the entity implement the IInactive interface.
// If yes, only return "active" row, otherwise return all rows
if (typeof(T)is IInactive)
{
// Problem: the code in this block never executes, that is,
// (typeof(T)is IInactive) never evaluates to true
...
}
return dbSet;
}
I would greatly appreciate some help solving this issue! Thanks.
where T : class, IInactiveand see where that takes you. Even if it doesn't solve this exact problem, you can skip the implementation check and prevent callers from using entity types that don't support this check (it's enforced by the compiler and the runtime!).