First of all, I want to say that I’m a beginner in .NET and I'm a beginner in english. :(
I have the following code in my controller: The intention is to send the data set to the clients. I need to make that treatment to provide a suitable visualization in the front end application. However, I noticed that the code execution is very slow especially when the first conditional is executed. The "result" variable contains all my data and in fact, its size is small, around 40 records only (result.Count() is 40).
var response = new List<object>();
//the results are retrieved quickly from the database
var result = (from a in obj.TBRelPadraoCorEstado
join b in obj.TBCorAlarme on a.idCor equals b.idCor
join c in obj.TBEstadoAlarme on a.idEstado equals c.idEstado
join d in obj.TBPadraoCor on a.idPadrao equals d.idPadraoCor
select new
{
idRelPadraoCor = a.idRelPadraoCor,
idPadrao = (int)d.idPadraoCor,
nomePadrao = d.nomePadrao,
idEstado = a.idEstado,
idCor = a.idCor,
hexCor = b.hexCor,
nomeCor = b.nomeCor,
estadoAlarme = c.estadoAlarme,
permiteReconhecimento = a.permiteReconhecimento,
isReconhecido = a.isReconhecido,
prioridade = a.prioridade
}).OrderBy(d => d.idPadrao).AsEnumerable();
int size = result.Count();
List<object> buffer = new List<object>();
for (int i = 0; i < size - 1; i++)
{
//at this point, the code execution become very slow.
if ((int)result.ElementAt(i).idPadrao == (int)result.ElementAt(i + 1).idPadrao)
{
buffer.Add(new
{
idRelPadraoCor = result.ElementAt(i).idRelPadraoCor,
idPadrao = result.ElementAt(i).idPadrao,
idEstado = result.ElementAt(i).idEstado,
idCor = result.ElementAt(i).idCor,
hexCor = result.ElementAt(i).hexCor,
nomeCor = result.ElementAt(i).nomeCor,
estadoAlarme = result.ElementAt(i).estadoAlarme,
permiteReconhecimento = result.ElementAt(i).permiteReconhecimento,
isReconhecido = result.ElementAt(i).isReconhecido,
prioridade = result.ElementAt(i).prioridade
});
}
else
{
buffer.Add(new
{
idRelPadraoCor = result.ElementAt(i).idRelPadraoCor,
idPadrao = result.ElementAt(i).idPadrao,
idEstado = result.ElementAt(i).idEstado,
idCor = result.ElementAt(i).idCor,
hexCor = result.ElementAt(i).hexCor,
nomeCor = result.ElementAt(i).nomeCor,
estadoAlarme = result.ElementAt(i).estadoAlarme,
permiteReconhecimento = result.ElementAt(i).permiteReconhecimento,
isReconhecido = result.ElementAt(i).isReconhecido,
prioridade = result.ElementAt(i).prioridade
});
var copyList = new List<object>(buffer);
response.Add(new { nomePadrao = result.ElementAt(i).nomePadrao, idPadrao = result.ElementAt(i).idPadrao, stateSet = copyList });
buffer = new List<object>();
}
}
return Ok(response);
To get a sense of my problem:
If I send only the “result” content, it takes somewhat 40 milliseconds. On the other hands, if I perform the snippet of the loop, it takes around 8000 or even 10000 milliseconds]1
Can someone tell me about what is happening?
Thanks for your patience.