我怎么能包括的属性包括沿谓语? [英] How can I include a predicate along with Included properties?

查看:124
本文介绍了我怎么能包括的属性包括沿谓语?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有一个功能,让我查询数据库,同时包括结果其他一些相关的表,以防止打到我知道如果我不这么做,否则发生数据库返回:

 公共类EntityRepository< TEntity> :IEntityRepository< TEntity>其中,TEntity:类
{
保护DbSet< TEntity> dbSet;

私人只读的DbContext直流电;

公共EntityRepository()
{
直流=新的DbContext(Utility.Config.GetEntityDbConnection());
dbSet = dc.Set< TEntity>();
}


公众的IQueryable< TEntity> GetAllIncluding(PARAMS表达式来; Func键< TEntity,对象>> [] includeProperties)
{
的foreach(VAR在includeProperties includedProperty)
{
dbSet.Include(includedProperty).Load ();
}

返回dbSet;
}
}



不过,我需要能够同时使用where子句之前的包括,以便它不会尝试查询整个数据库时,触发加载()方法。



我试图做这样的事情(这显然是行不通的,因为你可以重新分配 dbset 就像下面我的示例代码)。

 公开的IQueryable< TEntity> SearchForIncluding(表达式来; Func键< TEntity,布尔>>谓语,则params表达式来; Func键< TEntity,对象>> [] includeProperties)
{
dbset = dbSet.Where(谓语); //这不明明WORK

的foreach(在includeProperties VAR includedProperty)
{
dbSet.Include(includedProperty).Load();
}
}


解决方案

您可以使用 LinqKit 库。下载的NuGet包,并与使用 AsExpandable 扩展方法:

 公开的IQueryable< TEntity> SearchForIncluding(表达式来; Func键< TEntity,布尔>>谓语,则params表达式来; Func键< TEntity,对象>> [] includeProperties)
{
的IQueryable< TEntity>查询= dbSet;

如果(includeProperties!= NULL)
{
=查询includeProperties.Aggregate(查询(目前包括)=> current.Include(含));
}
如果(谓词!= NULL)
{
=查询query.AsExpandable(),其中(谓语)。
}
返回查询;
}


Currently I have a function that allows me to query the database while including some other related tables in the results to prevent a return hit to the database that I know would occur otherwise if I don't:

public class EntityRepository<TEntity> : IEntityRepository<TEntity> where TEntity : class
{
    protected DbSet<TEntity> dbSet;

    private readonly DbContext dc;

    public EntityRepository()
    {
        dc = new DbContext(Utility.Config.GetEntityDbConnection());
        dbSet = dc.Set<TEntity>();
    }


    public IQueryable<TEntity> GetAllIncluding(params Expression<Func<TEntity, object>>[] includeProperties)
    {
            foreach (var includedProperty in includeProperties)
            {
                dbSet.Include(includedProperty).Load();
            }

            return dbSet;
    }
}

However, I need to be able to also use a where clause prior to the include so that it doesn't try to query the entire database when it fires the Load() method.

I was trying to do something like this (which obviously doesn't work because you can reassign dbset like in my sample code below).

    public IQueryable<TEntity> SearchForIncluding(Expression<Func<TEntity, bool>> predicate, params Expression<Func<TEntity, object>>[] includeProperties)
    {
           dbset = dbSet.Where(predicate);  //THIS DOESN'T WORK OBVIOUSLY

           foreach (var includedProperty in includeProperties)
           {
                dbSet.Include(includedProperty).Load();
           }
    }

解决方案

You can use the LinqKit library. Download the nuget package and and use the AsExpandable extension method:

public IQueryable<TEntity> SearchForIncluding(Expression<Func<TEntity, bool>> predicate, params Expression<Func<TEntity, object>>[] includeProperties)
{
        IQueryable<TEntity> query = dbSet;

        if (includeProperties != null)
        {
            query = includeProperties.Aggregate(query, (current, include) => current.Include(include));
        }
        if (predicate != null)
        {
            query = query.AsExpandable().Where(predicate);
        }
        return query;
}

这篇关于我怎么能包括的属性包括沿谓语?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆