0

How do i pass a variable in the entity where, when this variable is null or empty, the query returns all data to me.

Example:

 public IList<Pessoas> Pessoas { get; set; }

 Pessoas = await _context.Pessoas
                 .Where(s => s.Nome == nome)
                .ToListAsync<Pessoas>();
Ravikumar
  • 613
  • 1
  • 9
  • 22
Renato
  • 119
  • 1
  • 1
  • 10
  • https://stackoverflow.com/questions/11194/conditional-linq-queries, https://stackoverflow.com/questions/7438382/linq-how-to-exclude-condition-if-parameter-is-null, – CodeCaster Aug 21 '18 at 12:59

2 Answers2

1

You can use double pipes(or ) || operator.

Pessoas = await _context.Pessoas
                 .Where(s => s.Nome == nome || string.IsNullOrEmpty(nome))
                .ToListAsync<Pessoas>();
maulik kansara
  • 1,087
  • 6
  • 21
0

i guess you can try something like

public IList<Pessoas> Pessoas { get; set; }

Pessoas = await _context.Pessoas
                 .Where(s => s.Nome == nome|| string.IsNullOrEmpty(nome))
                .ToListAsync<Pessoas>();