Convert several tables to json from a query in sql

2

Well I have not been able to achieve the conversion that I need and it seems to me that on this side I could work. Having already generated these classes with link

public class ProductoConsultaJ
{
    [JsonProperty("text")]
    public string Text { get; set; }
}

public class SubpartidaConsultaJ
{
    [JsonProperty("text")]
    public string Text { get; set; }

    [JsonProperty("nodes")]
    public List<ProductoConsultaJ> nodes { get; set; }
}

public class PartidaConsultaJ
{
    [JsonProperty("text")]
    public string Text { get; set; }

    [JsonProperty("nodes")]
    public List<SubpartidaConsultaJ> nodes { get; set; }
}

public class CapituloConsultaJ
{
    [JsonProperty("text")]
    public string Text { get; set; }

    [JsonProperty("nodes")]
    public List<PartidaConsultaJ> nodes { get; set; }
}

and having these related tables

Can someone guide me how should I fill the classes to generate this json?

I am trying to fill it in the following way:

using (SqlCommand command = new SqlCommand("SELECT DISTINCT TEMPORAL_CAPITULOS.ChapterCode, dbo.TEMPORAL_CAPITULOS.DescriptionSpa, dbo.TEMPORAL_CAPITULOS.DescriptionEng " + 
                        "FROM dbo.TEMPORAL_CAPITULOS INNER JOIN " + 
                        "dbo.TEMPORAL_PARTIDAS ON dbo.TEMPORAL_CAPITULOS.ChapterCode = dbo.TEMPORAL_PARTIDAS.ChapterCode INNER JOIN " + 
                        "dbo.TEMPORAL_SUBPARTIDAS ON dbo.TEMPORAL_PARTIDAS.HeadingCode = dbo.TEMPORAL_SUBPARTIDAS.HeadingCode INNER JOIN " + 
                        "dbo.TEMPORAL_PRODUCTOS ON dbo.TEMPORAL_SUBPARTIDAS.SubHeadingCode = dbo.TEMPORAL_PRODUCTOS.SubheadingCode", con))
                            {
                                using (SqlDataReader reader = command.ExecuteReader())
                                {
                                    List<CapituloConsultaJ> objListaCapitulos = new List<CapituloConsultaJ>();

                                    while (reader.Read())
                                    {
                                        CapituloConsultaJ objCapitulo = new CapituloConsultaJ();

                                        objCapitulo.Text = reader["DescriptionSpa"].ToString();
                                        objListaCapitulos.Add(objCapitulo);

                                        using (SqlCommand command2 = new SqlCommand("SELECT DISTINCT TEMPORAL_PARTIDAS.ChapterCode, dbo.TEMPORAL_PARTIDAS.DescriptionSpa, " +
                                        "dbo.TEMPORAL_CAPITULOS.DescriptionEng " +
                                        " FROM dbo.TEMPORAL_CAPITULOS INNER JOIN " +
                                        "dbo.TEMPORAL_PARTIDAS ON dbo.TEMPORAL_CAPITULOS.ChapterCode = dbo.TEMPORAL_PARTIDAS.ChapterCode INNER JOIN " +
                                        "dbo.TEMPORAL_SUBPARTIDAS ON dbo.TEMPORAL_PARTIDAS.HeadingCode = dbo.TEMPORAL_SUBPARTIDAS.HeadingCode INNER JOIN " +
                                        "dbo.TEMPORAL_PRODUCTOS ON dbo.TEMPORAL_SUBPARTIDAS.SubHeadingCode = dbo.TEMPORAL_PRODUCTOS.SubheadingCode " + 
                                        "where TEMPORAL_PARTIDAS.ChapterCode = '" + reader["ChapterCode"].ToString() + "'", con))
                                        {
                                            using (SqlDataReader reader2 = command2.ExecuteReader())
                                            {
                                                List<PartidaConsultaJ> objListaPartidas = new List<PartidaConsultaJ>();
                                                while (reader2.Read())
                                                {
                                                    PartidaConsultaJ objPartida = new PartidaConsultaJ();
                                                    objPartida.Text = reader2["DescriptionSpa"].ToString();
                                                   objCapitulo.nodes.Add(objPartida);



                                                }
                                            }
                                        }
                                    }

                                    getValue = JsonConvert.SerializeObject(objListaCapitulos);
                                }    
                }

And it generates an error in objCapitulo.nodes.Add (objPartida); How should that object be filled? Thanks

    
asked by Nelson Rodriguez 14.03.2018 в 02:37
source

1 answer

0

The error of Reference to object not established that you comment is because you have not initialized the collection "nodes" and therefore it is null. You can change the line where instances objCapitulo by CapituloConsultaJ objCapitulo = new CapituloConsultaJ() { nodes = new List<PartidaConsultaJ>()}; , or you can add a constructor to each class and initialize collections in it. Or depending on the version c # you're using, use the initializer on the property public List<PartidaConsultaJ> nodes { get; set; } = new List<PartidaConsultaJ>();

    
answered by 15.03.2018 / 12:44
source