Esteban V Esteban V - 3 months ago 16
C# Question

Convert list of an entity to an array of ints

I need to fill an array with 60 values type int, and I have the values inside a DbSet in the database.

Without making a loop, is there a way I can conver this List to a Int[].
The value is in a property called temperature

public void SetLineChartData()
{
//Suppose we have a list of 60 items.
using (ZigBeeContext db = new ZigBeeContext())
{
var lista = (from p in db.Medidas
select new Medida
{
Fecha = p.FechaHora,

}).ToList();
}



lineChartData = new int[60];
lineChartData[0] = RandomNumberGenerator.randomScalingFactor();
hora[0] = DateTime.Now.ToShortTimeString();

lineChartData[1] = RandomNumberGenerator.randomScalingFactor();
hora[1] = DateTime.Now.ToShortTimeString();

lineChartData[2] = RandomNumberGenerator.randomScalingFactor();
hora[2] = DateTime.Now.ToShortTimeString();

lineChartData[3] = RandomNumberGenerator.randomScalingFactor();
hora[3] = DateTime.Now.ToShortTimeString();

lineChartData[4] = RandomNumberGenerator.randomScalingFactor();
hora[4] = DateTime.Now.ToShortTimeString();

lineChartData[5] = RandomNumberGenerator.randomScalingFactor();
hora[5] = DateTime.Now.ToShortTimeString();

lineChartData[6] = RandomNumberGenerator.randomScalingFactor();
hora[6] = DateTime.Now.ToShortTimeString();


//colorString = "rgba(" + RandomNumberGenerator.randomColorFactor() + "," + RandomNumberGenerator.randomColorFactor() + "," + RandomNumberGenerator.randomColorFactor() + ",.3)";
}


I need to fill an array with 60 values type int, and I have the values inside a DbSet in the database.

Without making a loop, is there a way I can conver this List to a Int[]. The value is in a property called temperature

Answer

Not sure what you mean by "Without making a loop" as many functions will perform loops even if they don't look like it. If I understand what you are trying to do, then maybe something like this:

int[] myArray = lista.Select(x => x.temperature).ToArray();
Comments