查询 MVC {搜索参数}
本文关键字:参数 搜索 MVC 查询 | 更新日期: 2023-09-27 18:35:22
我有这个服务"搜索"
public IEnumerable<PainelChamada> Search(PainelChamadaParamenters parameters)
{
var query = base.context.PaineisChamada.AsQueryable();
if (parameters.Id.HasValue)
query = query.Where(x => x.Id == parameters.Id);
if (parameters.DataAcessoInicio.HasValue)
query = query.Where(x => x.DataModificacao == parameters.DataAcessoInicio);
if (parameters.DataAcessoFim.HasValue)
query = query.Where(x => x.DataModificacao == parameters.DataAcessoFim);
if (!string.IsNullOrEmpty(parameters.Descricao))
query = query.Where(x => x.Descricao.Contains(parameters.Descricao));
return query;
}
我需要在服务中插入"Fornecedores",但"Fornecedores"是列表。
public string[] Fornecedores { get; set; }
怎么做?
我试过了,但没有成功...
您应该能够将其添加为参数对象PainelChamadaParamenters的另一个属性,然后在搜索函数中,您可以按如下方式使用它,假设这是您的数据模型的样子:
public IEnumerable<PainelChamada> Search(PainelChamadaParamenters parameters)
{
var query = base.context.PaineisChamada.AsQueryable();
if (parameters.Id.HasValue)
query = query.Where(x => x.Id == parameters.Id);
if (parameters.DataAcessoInicio.HasValue)
query = query.Where(x => x.DataModificacao == parameters.DataAcessoInicio);
if (parameters.DataAcessoFim.HasValue)
query = query.Where(x => x.DataModificacao == parameters.DataAcessoFim);
if (!string.IsNullOrEmpty(parameters.Descricao))
query = query.Where(x => x.Descricao.Contains(parameters.Descricao));
if (parameters.Fornecedores.Length > 0)
query = query.Where(x => parameters.Fornecedores.ToList().Contains(x.Fornecedor));
return query;
}