private void CarregarConsulta(int id = 0) { var filtro = new VeiculoFiltro(); if (cbCampos.SelectedIndex == 0 && (!string.IsNullOrWhiteSpace(txtTexto.Text))) { filtro.Id = Funcoes.StrToIntDef(txtTexto.Text, 0); } if (cbCampos.SelectedIndex == 1) { filtro.Placa = txtTexto.Text; } if (cbCampos.SelectedIndex == 2) { filtro.DescMarca = txtTexto.Text; } if (cbCampos.SelectedIndex == 3) { filtro.Modelo = txtTexto.Text; } if (cbCampos.SelectedIndex == 4) { filtro.Ano = txtTexto.Text; } dgvDados.DataSource = _session.ServiceVeiculo.Filtrar(filtro, id); }
public IEnumerable <VeiculoConsulta> Filtrar(VeiculoFiltro filtro, int id = 0) { var resultado = _repositorioVeiculo.RetornarConsulta(); if (id > 0) { resultado = resultado.Where(x => x.Id == id); } else { if (!string.IsNullOrWhiteSpace(filtro.DescMarca)) { resultado = resultado.Where(x => x.DescricaoMarca.Contains(filtro.DescMarca.ToUpper())); } if (!string.IsNullOrWhiteSpace(filtro.Ano)) { resultado = resultado.Where(x => x.Ano == filtro.Ano); } if (!string.IsNullOrWhiteSpace(filtro.Modelo)) { resultado = resultado.Where(x => x.Modelo.Contains(filtro.Modelo.ToUpper())); } if (!string.IsNullOrWhiteSpace(filtro.Placa)) { resultado = resultado.Where(x => x.Placa.Contains(filtro.Placa.ToUpper())); } if (filtro.Id > 0) { resultado = resultado.Where(x => x.Id == filtro.Id); } resultado = resultado.OrderBy(x => x.Id); } return(resultado.ToList()); }
public List <VeiculoConsulta> FiltrarDados(VeiculoFiltro filtro, int id = 0) { using (_banco = new BancoFB()) { var sb = new StringBuilder(); sb.AppendLine("SELECT V.ID, V.PLACA, V.MODELO, V.ANO, M.DESCRICAO FROM Veiculo V"); sb.AppendLine(" INNER JOIN MARCA M ON V.ID_MARCA = M.ID"); sb.AppendLine(" WHERE V.ID IS NOT NULL"); if (filtro.Id > 0) { sb.AppendLine(" AND V.ID = " + filtro.Id); } if (!string.IsNullOrWhiteSpace(filtro.DescMarca)) { sb.AppendLine(" AND M.DESCRICAO containing('" + filtro.DescMarca + "')"); } if (!string.IsNullOrWhiteSpace(filtro.Ano)) { sb.AppendLine(" AND V.ANO = '" + filtro.Ano + "'"); } if (!string.IsNullOrWhiteSpace(filtro.Modelo)) { sb.AppendLine(" AND V.MODELO containing('" + filtro.Modelo + "')"); } if (!string.IsNullOrWhiteSpace(filtro.Placa)) { sb.AppendLine(" AND V.PLACA containing('" + filtro.Placa + "')"); } if (id > 0) { sb.AppendLine(" AND V.ID = " + id); } sb.AppendLine(" ORDER BY V.ID"); _banco.RetornoReader(sb.ToString()); List <VeiculoConsulta> lista = new List <VeiculoConsulta>(); while (_banco.Read()) { var model = new VeiculoConsulta() { Id = _banco.CampoInt32("Id"), Ano = _banco.CampoStr("Ano"), Modelo = _banco.CampoStr("Modelo"), Placa = _banco.CampoStr("Placa"), DescricaoMarca = _banco.CampoStr("Descricao") }; lista.Add(model); } ; _banco.CloseReader(); return(lista); } }