Reputation: 4660
I am using LinQ to XML to populate a dropdown list when my page loads. How can I cache the results so that I don't have to run the query each and every time the page loads? Also the xml file will only be updated once a day. Is it better to cache or just read it each time?
Upvotes: 1
Views: 281
Reputation: 1501013
Call ToList()
on the results of the query. Then cache the results in a static variable, accessed in a thread-safe way:
private static List<Whatever> dropDownListValues;
private static object listLock = new object();
public static IList<Whatever> DropDownListValues
{
get
{
lock(listLock)
{
if (dropDownListValues == null ||
DetectValuesChanged()) // However you implement this!
{
dropDownListValues = // insert your query here
.ToList();
}
return dropDownListValues;
}
}
}
Upvotes: 1