Doubt with Array in C# returning data in Json

Asked

Viewed 375 times

2

I have a structure that returns me the results of the header and detail, only that the records duplicates according to the details, as I could bring only 1 time the header, I appreciate the help.

//consulta o jogo completo passando o ID do jogo
//http://webapi.sistemaguardiao.com.br/api/consulta/ListarJogoCompleto/84
[HttpGet]
[Route("consulta/ListarJogoCompleto/{idjogo}")]
public HttpResponseMessage ListarJogoCompleto(int idjogo)
{
    try
    {
        var tTabela = new JogoCompleto();
        var listar = tTabela.ListarJogoCompleto(idjogo);
        return Request.CreateResponse(HttpStatusCode.OK, new { jogo  = listar.ToArray() });
    }
    catch (Exception ex)
    {
        return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
    }
}

namespace Generico.Aplicacao
{
    public class JogoCompleto
    {
        private Contexto contexto;

        public List<TB_VIW_JOGO_COMPLETO> ListarJogoCompleto(int idjogo)
        {
            var strQuery = "";
            strQuery += "select   exemplo                ";

            using (contexto = new Contexto())
            {
                var retornoDataReader = contexto.ExecutaComandoComRetorno(strQuery);
                return TransformaReaderEmListaObjetos(retornoDataReader);
            }
        }

        private List<TB_VIW_JOGO_COMPLETO> TransformaReaderEmListaObjetos(SqlDataReader reader)
        {
            var retornando = new List<TB_VIW_JOGO_COMPLETO>();

            while (reader.Read())
            {
                TB_VIW_JOGO_COMPLETO tabela = new TB_VIW_JOGO_COMPLETO()
                {
                    RAZAO_SOCIAL = reader["RAZAO_SOCIAL"] == DBNull.Value ? string.Empty : reader["RAZAO_SOCIAL"].ToString(),
                    DATA_JOGO = reader["DATA_JOGO"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(reader["DATA_JOGO"]),
                    NOME_VENDEDOR = reader["NOME_VENDEDOR"] == DBNull.Value ? string.Empty : reader["NOME_VENDEDOR"].ToString(),
                    NUMERO_TERMINAL = reader["NUMERO_TERMINAL"] == DBNull.Value ? string.Empty : reader["NUMERO_TERMINAL"].ToString(),
                    NUMERO_JOGO = reader["NUMERO_JOGO"] == DBNull.Value ? string.Empty : reader["NUMERO_JOGO"].ToString(),
                    HORA_IMPRESSAO = reader["HORA_IMPRESSAO"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(reader["HORA_IMPRESSAO"]),
                    ESTRACAO = reader["ESTRACAO"] == DBNull.Value ? string.Empty : reader["ESTRACAO"].ToString(),
                    VALOR_JOGADO = reader["VALOR_JOGADO"] == DBNull.Value ? 0 : Convert.ToDecimal(reader["VALOR_JOGADO"]),
                    CODIGO_NCU = reader["CODIGO_NCU"] == DBNull.Value ? string.Empty : reader["CODIGO_NCU"].ToString(),
                    OPERACAO = reader["OPERACAO"] == DBNull.Value ? string.Empty : reader["OPERACAO"].ToString(),
                    NUMERO_JOGO_DETALHE = reader["NUMERO_JOGO_DETALHE"] == DBNull.Value ? string.Empty : reader["NUMERO_JOGO_DETALHE"].ToString(),
                    DESCRICAO_PREMIO = reader["DESCRICAO_PREMIO"] == DBNull.Value ? string.Empty : reader["DESCRICAO_PREMIO"].ToString(),
                    VALOR_JOGO = reader["VALOR_JOGO"] == DBNull.Value ? 0 : Convert.ToDecimal(reader["VALOR_JOGO"]),
                    VALOR_TOTAL = reader["VALOR_TOTAL"] == DBNull.Value ? 0 : Convert.ToDecimal(reader["VALOR_TOTAL"])

                };

                retornando.Add(tabela);
            }
            reader.Close();
            return retornando;
        }
    }
}
  • more when you do a database query between two tables, it will repeat the records up to the total number of detail records,

  • i don’t want to read the json, I created it, see my code.

  • What about the return? How should it look?

  • Do you want to "RAZAO_SOCIAL" only appear once on the first record, instead of all records. This?

  • can be, this way I’ll know how to add other fields

1 answer

1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.