简体   繁体   English

通用存储库筛选器子属性

[英]Generic Repository Filter Child Properties

I am using a generic repository to query my database here . 我使用的是通用的存储库查询我的数据库在这里 All my entities inherit a base class which has a property Active , I set Active to false when the user deletes it. 我所有的实体都继承了一个具有Active属性的基类,当用户删除Active ,我将Active设置为false The problem I am facing now is that while the code below works fine for main entity I could not figure out how to return only Active child activities. 我现在面临的问题是,尽管下面的代码对于主实体很好用,但我无法弄清楚如何仅返回Active子活动。

I have tried using dynamic linq but not successfully. 我尝试使用动态linq,但未成功。 Is there any way to do this except the most obvious using linq on child entities? 除了最明显的在子实体上使用linq之外,还有什么方法可以做到这一点?

 public virtual TEntity GetById(object id,
        Expression<Func<TEntity, bool>> filter = null,
        string includeProperties = "")
    {
        IQueryable<TEntity> query = DbSet;

        if (filter != null)
        {
            query = query.Where(filter);
        }
        if (includeProperties.Length > 0)
        {
            query = includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                .Aggregate(query, (current, includeProperty) => current.Include(includeProperty));
        }
        else
        {
            return DbSet.Find(id);
        }

        if (query.Any())
        {
            return query.First();
        }
        return query.First();
    }

You should create filter for that ( EntityFramework.Filters ): 您应该为此创建过滤器( EntityFramework.Filters ):

public interface IActive
{
    public bool Active {get;set;}
}

public class MyClass : IActive
{
    public int Id {get;set;}
    public bool Active {get;set;}
}

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{   
    modelBuilder.Conventions
       .Add(FilterConvention.Create<IActive, bool>("MyFilter", (entity, Active) => entity.Active);
}

Usage: 用法:

var filter = context.EnableFilter("MyFilter");
filter.SetParameter("Active", true);

var item = GetById(1);
//now inside GetById method 'item' and all it's children will be filtered with "Active == true" condition

context.DisableFilter("MyFilter");

One of options : 选项之一:

public virtual TEntity GetById(object id,
        Expression<Func<TEntity, bool>> filter = null,
        string includeProperties = "")
    {
        IQueryable<TEntity> query = GetActive();

        if (filter != null)
        {
            query = query.Where(filter);
        }
        if (includeProperties.Length > 0)
        {
            query = includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                .Aggregate(query, (current, includeProperty) => current.Include(includeProperty));
        }
        else
        {
            return DbSet.Find(id);
        }

        if (query.Any())
        {
            return query.First();
        }
        return query.First();
    }

public virtual IQueryable<TEntity> GetActive()
{
    return 
        DbSet.Where(a => a.Active);
}

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

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