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