Search code examples
asp.netlinqdbcontext

Query DbContext with Linq conditions and async


so I have a line of code as such:

Portfolio portfolio = await _dbContext.Portfolios.FindAsync(id);

But I only want portfolios that have their IsDeleted value set to false.

And I need it to be async, so something like this :

Portfolio portfolio = await _dbContext.Portfolios.FindAsync(id).Where(p => p.IsDeleted == false);

Is this possible at all?


Solution

  • Is it this, what you are looking for.

    Portfolio portfolio = await _dbContext.Portfolios.FirstOrDefaultAsync(p => p.Id &&  IsDeleted == false);
    

    If you are looking for list of portfolio that you will do it like this

    List<Portfolio> portfolios = await _dbContext.Portfolios.Where(p => p.Id &&  IsDeleted == false).ToList();