Using the extension method in the base class in a LINQ query

Sorry in advance for my naivety.

I use the Entity Framework to store objects that I defined in my domain model. The objects of my domain model all inherit from my EntityBase class. This property has a desire to be common to all of my entities:

public class EntityBase
{
    public string CreatedBy { get; set; }
    public DateTime? Created { get; set; }

    public int ModifiedBy { get; set; }
    public DateTime? Modified { get; set; }

    public bool Enabled { get; set; }
    public bool Deleted { get; set; }
}

Now that I want to query EF using LINQ, it would be nice if I didn't have to include elements to check if any particular object is included or not. Each request includes a code, for example:

var messages = _db.Memberships.Where(m => m.UserId.Equals(userId))
                              .SelectMany(m => m.Group.Messages)
                              .Include(m => m.Group.Category)
                              .Select(m => m.Enabled && !m.Deleted) 
                              .ToList();

Instead of doing this every time, I thought I would write an extension method that would act on IQueryable

public static IQueryable<EntityBase> Active(this IQueryable<EntityBase> entityCollection)
    {
        return entityCollection.Where(e => e.Enabled && !e.Deleted);
    }

, LINQ, , EntityBase - :

var messages = _db.Memberships.Where(m => m.UserId.Equals(userId))
            .SelectMany(m => m.Group.Messages)
            .Include(m => m.Group.Category)
            .Active() <============================= Extension Methd
            .ToList();

        return Mapper.Map<List<Message>,List<MessageDto>>(messages);

, :

Error 2 Argument 1: cannot convert from
          'System.Collections.Generic.List<Diffusr.Business.Entities.EntityBase>' to
          'System.Collections.Generic.List<Diffusr.Business.Entities.Message>'  

. , , .. ? , ?

+4
2

generics, :

public static IQueryable<T> Active<T>(this IQueryable<T> entityCollection) where T:EntityBase
{
    return entityCollection.Where(e => e.Enabled && !e.Deleted);
}

, .NET , 4.0. 4.0 (.. , ).

4.0, , , , - . Jon Skeet

+3

:

public static IQueryable<T> Active(this IQueryable<T> entityCollection)
    where T : EntityBase
{
    return entityCollection.Where(e => e.Enabled && !e.Deleted);
}
+3

All Articles