简体   繁体   中英

Filter generic included entities in Entity Framework 6

I would like to know if the following scenario would be possible: I created a generic repo for handling CRUD operation on multiple entities. Given that I have soft delete implementation on the entities, for a certain entity that relates with other entities, I would like to filter the included entities based on a property, let's say, IsDeleted.

public abstract class Repository<T, TOrderBy> where T : BaseEntity
{
   ...
    protected IQueryable<T> Get(Expression<Func<T, bool>> filter = null,
        params Expression<Func<T, object>>[] includes)
    {
        var query = Set.AsQueryable();

        if (filter != null)
        {
            query = query.Where(x => !x.DeletedAt.HasValue).Where(filter);
        }

        if (includes != null)
        {
            foreach (var include in includes)
            {
                query = query.Include(include); // In here i would like to also filter the included entities based on the IsDeleted property
            }
        }

        return query;
     }
}

The BaseEntity class looks like this, and all classes (including the ones that I would like to filter in the Include method) inherit from it:

 public abstract class BaseEntity
 {
     public bool IsDeleted { Get; Set; }
 }

I know that the included entities can be filter on the level where the concrete entity is available, but I was wondering if this is possible on the generic repository class, so I don't need to check the IsDeleted property for each query in the specific repositories.

Thanks.

Let's say you have two classes Foo and FooContainer like this:

    public class Foo : BaseEntity
    {
        public int Id { get; set; }
    }
    public class FooContainer : BaseEntity
    {
        public int Id { get; set; }
        public virtual Foo Foo { get; set; }
    }

and a repo:

    public class FooContainerRepository : Repository<FooContainer, object>
    {
        public IQueryable<FooContainer> GetFooContainers(Expression<Func<FooContainer, bool>> filter = null,
         params Expression<Func<FooContainer, object>>[] includes) => base.Get(filter, includes);
    }

In order to get FooContainer entities with Foo included you would call the repo like this:

            var repo = new FooContainerRepository();
            repo.GetFooContainers(null, fc => fc.Foo);

Moving on to Get method in Repository class:

                foreach (var include in includes)
                {
                    query = query.Include(include); // *include coment*
                }

include coment - at this point you only know that include is Func that thats FooContainer as a param and returns an object . Even though u know that object has the IsDeleted flag, its still just an object - meaning that with current Get method signature it is not possible to add Where filtering to included entities.

In order to do that you would have to provide Expression<Func<T, bool>> includeFilter for each included entity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM