查询MVC {搜索参数}

时间:2014-12-24 13:19:07

标签: c# sql asp.net-mvc asp.net-mvc-4

我有这项服务“搜索”

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; }

怎么做?

我试过了,但没有成功......

1 个答案:

答案 0 :(得分:0)

您应该能够将其添加为您的Parameters对象的另一个属性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;
    }