.net&实体框架:使用Iqueryable查询数据库

发布于 2025-02-02 16:53:17 字数 679 浏览 4 评论 0原文

我有一种方法,该方法采用了iqueryable< tentity>在应用我的所有过滤器的地方,现在我想通过使用此iQueryable来查询数据库,

例如我所定义的iqueryable,例如:

IQueryable<TEntity> query = Enumerable.Empty<TEntity>().AsQueryable();

然后您可以:例如:

query.Where(q => q.Parameter == true)

然后,它应该将空集合传输到应该查询数据库

代码的存储库:

protected EntityDBContext _context;
protected DbSet<TEntity> _set;

public async Task<List<TEntity>> ReadAsync(IQueryable<TEntity> queryable) 
{
    // something like this but by using the dbset
    return queryable.ToList();
}

还是我应该合并所有过滤器然后查询数据库?

谢谢

I have a method which takes an IQueryable<TEntity> where all my filters have been applied and now I want to query the database by using this IQueryable

The IQueryable i defined like:

IQueryable<TEntity> query = Enumerable.Empty<TEntity>().AsQueryable();

and then you can for example :

query.Where(q => q.Parameter == true)

and then it should transfer the empty collection to the repository where it should query the database

Code:

protected EntityDBContext _context;
protected DbSet<TEntity> _set;

public async Task<List<TEntity>> ReadAsync(IQueryable<TEntity> queryable) 
{
    // something like this but by using the dbset
    return queryable.ToList();
}

Or should I just merge all the filters and then query the database?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不忘初心 2025-02-09 16:53:18

您可以使用 func 委托使用通用过滤器,

public virtual IQueryable<TEntity> GetWithCondition(Expression<Func<TEntity, bool>> expression)
        {
            return _dbContext.Set<TEntity>().Where(expression);
        }

可以将此方法称为此类方法。它也将接受多种条件。

public List<EntityNameModel> GetList(bool parameter)
        {
            return GetWithCondition(x => x.Parameter == true).ToList();
            
        }

这基本上将返回记录列表。如果您不想通过任何条件,那么它将返回整个实体记录列表。

You can use generic filters using Func delegates like this

public virtual IQueryable<TEntity> GetWithCondition(Expression<Func<TEntity, bool>> expression)
        {
            return _dbContext.Set<TEntity>().Where(expression);
        }

You can call this method like this. It will accept multiple conditions as well.

public List<EntityNameModel> GetList(bool parameter)
        {
            return GetWithCondition(x => x.Parameter == true).ToList();
            
        }

This will basically return the list of records. If you don't want to pass any condition then it will return whole list of entity records.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文