简体   繁体   English

EF Core 过滤包含后的子实体

[英]EF Core filter the child entities after include

I am working with EF Core and I created a generic repository.我正在使用 EF Core 并创建了一个通用存储库。 I have a method which returns all the entities with their child entities.我有一个方法可以返回所有实体及其子实体。 Here is my method:这是我的方法:

public Repository(DevbAERPContext dbContext)
    {
        _dbContext = dbContext;
        Table = _dbContext.Set<T>();
    }

public async Task<IEnumerable<T>> GetAllWithInclude(Expression<Func<T, bool>> where, string[] includeProperties)
    {
        var result = includeProperties.Aggregate(Table.Where(where), (query, path) => query.Include(path)).AsNoTracking();
        return await result.ToListAsync();
    }

While using this method I don't want to get the soft deleted data.使用此方法时,我不想获取软删除的数据。 I can filter the parent entity by writing where expression but I also want to do the same for the child entities.我可以通过编写 where 表达式来过滤父实体,但我也想对子实体做同样的事情。 Currently I can handle the issue in controller like this:目前我可以像这样处理 controller 中的问题:

var roles = await _roleRepository.GetAllWithInclude(x => !x.IsDeleted, new string[] { "RoleTemplateSkills", "RoleTemplateSkills.Skill" }).ConfigureAwait(false);
        var mappedRoles = _mapper.Map<List<RoleTemplateViewModel>>(roles);
        foreach(var mappedRole in mappedRoles)
        {
            mappedRole.RoleTemplateSkills = mappedRole.RoleTemplateSkills.Where(x => !x.IsDeleted).ToList();
        }

What I want is to do this filtering in my generic repository method.我想要的是在我的通用存储库方法中进行此过滤。 Is there any way to do it?有什么办法吗?

I'm not sure if I got your problem exactly.我不确定我是否确实遇到了您的问题。 But here below is a sample code I used in one generic repository may help you.但下面是我在一个通用存储库中使用的示例代码可能会对您有所帮助。

public TEntity FindByInclude(Expression<Func<TEntity, bool>> predicate, params Expression<Func<TEntity, object>>[] includeProperties)
        {
            IQueryable<TEntity> result = dbSet.Where(predicate);


            if (includeProperties.Any())
            {
                foreach (var includeProperty in includeProperties)
                {
                    result = result.Include(includeProperty);
                }
            }

            var firstResult = result.FirstOrDefault(predicate);

            if (firstResult != null)
                dbSet.Attach(firstResult);

            return firstResult;
        }

        public IEnumerable<TEntity> GetAllIncluding(params Expression<Func<TEntity, object>>[] includeProperties)
        {
            IQueryable<TEntity> result = dbSet.AsNoTracking();

            return includeProperties.Aggregate(result, (current, includeProperty) => current.Include(includeProperty));
        }

However, In the constructor, I didn't use Table.但是,在构造函数中,我没有使用 Table。 Instead I used as the following相反,我使用如下

public class FitnessRepository<TEntity> : IFitnessRepository<TEntity> where TEntity :class {
    private readonly FitnessDbContextt fitnessDbContext;

    private readonly DbSet<TEntity> dbSet;

    public FitnessRepository (FitnessDbContextt context) {
        fitnessDbContext = context;
        dbSet = context.Set<TEntity> ();

    }
}

I hope it helps.我希望它有所帮助。

Have you tried to add a filter to the Entity it self in the Entity designer?您是否尝试在实体设计器中将过滤器添加到它自己的实体? so you can skip adding the condition here.所以你可以跳过在这里添加条件。

Thanks谢谢

向实体添加条件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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