I am generating 2 queries using 1 argument, the first is a stored procedure that is responding to me as follows:
QUERY
var resultadoSP = (from bt in bd.ListadoGeneradoOrdenes.FromSqlRaw("EXECUTE BuscarDatos " + @clave) select new ListadoGeneradoOrdenes {
Clave = bt.Clave,
Cliente = bt.Cliente,
Num_ClienteL = bt.Num_ClienteL,
FechaRealizo = bt.FechaRealizo,
Num_Trabajador = bt.Num_Trabajador,
Nom_Trabajador = bt.Nom_Trabajador,
TipoUnidad = bt.TipoUnidad,
Operador = bt.Operador,
Responsable = bt.Responsable,
Direccion = bt.Direccion,
Correo = bt.Correo
}).ToList();
In his response, it is the following:
[{
"clave": "000001014147002000439710",
"cliente": "cliente 1",
"num_ClienteL": "0000100108",
"fechaRealizo": "2022-07-21T09:50:50.483",
"num_Trabajador": "1111",
"nom_Trabajador": "trabajador 1",
"tipoUnidad": "PLATAFORMA",
"operador": "OPERADOR DE PRUEBA",
"responsable": "RESPONSABLE",
"direccion": "ACTUALIZAR",
"correo": "ACTUALIZAR"
}]
regarding the following query is like this:
var resultadoBD = (from b in bd.OrdenesPt where b.CveCompuesta.Equals(clave) select new {
Ot = b.Ot,
Numero_Cliente = b.Numero_Cliente,
Pedido = b.Pedido,
Posicion = b.Posicion,
Sociedad = b.Sociedad,
CodigoMaterial = b.CodigoMaterial,
Material = b.Material,
Cantidad = b.Cantidad
}).ToList();
with your response like this:
[{
"ot": "1",
"numero_Cliente": "0000100108",
"pedido": "0020004397",
"posicion": 10,
"sociedad": "1",
"codigoMaterial": "1",
"material": "1",
"cantidad": 1
},
{
"ot": "2",
"numero_Cliente": "0000100108",
"pedido": "0020004397",
"posicion": 30,
"sociedad": "1",
"codigoMaterial": "1",
"material": "1",
"cantidad": 1
}
//Hasta n posiciones
]
To which with respect to joining it, I mean that it gives me an answer like this:
[{
"clave": "000001014147002000439710",
"cliente": "cliente 1",
"num_ClienteL": "0000100108",
"fechaRealizo": "2022-07-21T09:50:50.483",
"num_Trabajador": "1111",
"nom_Trabajador": "trabajador 1",
"tipoUnidad": "PLATAFORMA",
"operador": "OPERADOR DE PRUEBA",
"responsable": "RESPONSABLE",
"direccion": "ACTUALIZAR",
"correo": "ACTUALIZAR"
"Partidas": [{
"ot": "1",
"numero_Cliente": "0000100108",
"pedido": "0020004397",
//Demas propiedades
},
{
//N registros
}
//Hasta n posiciones
]
}]
In some forums that I consulted, they mentioned making use of Intersect
which I tried with var union = resultadoSP.Intersect(resultadoBD);
, which generates an error since the intersection cannot be performed, because it has a different structure in its class, hence try to convert it to JSON format, with this microsoft guide specifically where it mentions in Serialization example , but even so I have not managed to convert the response.
UPDATE
I found a way to do it, I did the following:
var ls = resultadoSP.Zip(resultadoBD, (a, b) =>
new {
Clave = a.Clave,
Cliente = a.Cliente,
//Varias Propiedades ...
ListadoPartidas = new List < OrdenesPt > {
new OrdenesPt {
Ot = b.Ot,
Numero_Cliente = b.Numero_Cliente,
Pedido = b.Pedido,
Posicion = b.Posicion,
//Más propiedades ...
}
}
}).ToList();
It prints me as I want, but now the drawback is that I cannot iterate the number of records stored in resultadoBD
(an example 3), it only accesses the first record
Solution
Where I described that it only accessed the first record, what I did was just point to the first query and it was as follows:
The important key is that through using Zip, I was able to join the 2 answers and to be able to reach the request that I was considering, it is that reading in some forums was that they formatted the answer. For this I did that:
Something that was a solution if it only had one record in the list, which is random, that this length is obtained depending on the number of records found from the first answer, for this, following the idea, it is enough to assign the answer obtained from:
and get to the join as needed the answer.