如何在实体中传递变量null

时间:2018-08-21 12:55:03

标签: asp.net entity-framework asp.net-core

如何在实体中传递变量,当该变量为null或空时,查询将所有数据返回给我。

示例:

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

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

2 个答案:

答案 0 :(得分:1)

您可以使用双管道(或)||运算符。

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

答案 1 :(得分:0)

我猜你可以尝试类似的东西

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

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