How to check if the query did not return data?

1

I have a query is would like to know if it returned data.

//classe aplicação-
private List<tb_cabecalho> TransformaReaderEmListaObjetos(SqlDataReader reader)
{
    var retornando = new List<tb_cabecalho>();
    while (reader.Read())
    {

        tb_cabecalho tabela = new tb_cabecalho()
        {
            campofixo = "H",
            cnpjdoecomerce = "61549259000180",
            dataincial = reader["DataInicial"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(reader["DataInicial"]),
            datafinal = reader["DataFinal"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(reader["DataFinal"])
        };

        retornando.Add(tabela);
    }
    reader.Close();
    return retornando;
}

    /// <summary>
    /// Aqui estou retornando os dados do cabecalho
    /// </summary>
    var tbuscar = new cabecalhoAplicacao();
    var retorno = tbuscar.DadosDoCabecalho();
    
asked by anonymous 07.10.2016 / 16:38

2 answers

2

If you want to check if your method is returning value, the simplest thing to do in this case is to use the if lista == null is a Count () % w / w

List<tb_cabecalho> lista = TransformaReaderEmListaObjetos(reader);
if(lista != null && lista.Count > 0)
{
   // Todo... 
}

Or just use Any ()

if(lista.Any())
{
   // Todo... 
}
    
07.10.2016 / 17:51
0

You can check if your query returned any line:

if(!reader.HasRow) return null;

On the other hand, where you use the method, just check if the return is other than null . You should do this before while

    
07.10.2016 / 16:42