I want to make the LINQ query in this method faster:
public string GeneraCodiceListaEventi(DateTime data)
{
string codice = String.Empty;
string[] range1 = new string[] { "08:00", "08:30", "09:00", "09:30", "10:00", "10:30", "11:00", "11:30", "12:00", "12:30", "13:00", "13:30", "14:00", "14:30", "15:00", "15:30", "16:00", "16:30", "17:00", "17:30", "18:00", "18:30", "19:00", "19:30", "20:00" };
string[] range2 = new string[] { "08:29", "08:30", "09:29", "09:59", "10:29", "10:59", "11:29", "11:59", "12:29", "12:59", "13:29", "13:59", "14:29", "14:59", "15:29", "15:59", "16:29", "16:59", "17:29", "17:59", "18:29", "18:59", "19:29", "19:59", "20:29" };
using (DatabaseDataContext contestoDB = new DatabaseDataContext())
{
contestoDB.ObjectTrackingEnabled = false;
for(int i=0; i<25; i++)
{
var eventi = (from db in contestoDB.Eventi
where db.DataPrenotazione.Date == data.Date && (db.DataPrenotazione.TimeOfDay >= TimeSpan.Parse(range1[i]) && db.DataPrenotazione.TimeOfDay <= TimeSpan.Parse(range2[i]))
select new
{
ID = db.ID,
IDCliente = db.IDCliente,
Note = db.Note,
Ore = db.DataPrenotazione.ToShortTimeString()
});
if (eventi.Any())
{
codice += "<li><span class='ora'>" + range1[i] + "</span><input type='checkbox' id='item-" + GetNumItem(range1[i]) + "'/><label for='item-" + GetNumItem(range1[i]) + "'>Espandi</label><ul>";
foreach (var e in eventi)
{
codice += "<li class='app'> " + e.Ore + " - " + GetNominativoClienteDaID(e.IDCliente) + CheckNota(e.Note);
}
codice += "</ul></li>";
}
else
{
codice += "<li><span class='ora'>" + range1[i] + "</span>" + noapp + "</li>";
}
}
}
return codice;
}
In this function I'll build a string to send to html using ajax and show in the browser. But how can I make the query faster? Is there an alternative?