Mr.Gomer
Mr.Gomer

Reputation: 649

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?

Upvotes: 2

Views: 167

Answers (1)

sairfan
sairfan

Reputation: 1062

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();

Upvotes: 1

Related Questions