caching gridview data in session
I have a reporting page with a gridview and a calendar. When the user loads the page, the default date is today and when he clicks on a date in the calendar control, it loads the reporting data for that day. Each user has a different report and the data is between 300-500 rows per day.
At the moment, I have a linq query that returns the data for a day in the form of a list. Given the relatively small amount of data per query, I'd like to store the result of the query in the session so tha开发者_StackOverflow中文版t I can do the paging and sorting from the session, without going back to the data store.
What I'm looking to do is a list of lists so that when the query loads, the data is saved until the session times out. For instance, if the user selects 4-5 different dates, each of these queries is saved in the session and then if he clicks on one of those dates again, a function first checks to see if the data is in the session: GetQueryFromSession( DateTime TheDate).
I'm stuck on this. How do you create a list of lists that works with the session object. Thanks.
store a dictionary in the session
private Dictionary<DateTime, System.Data.DataTable> Data
{
get
{
if (Session["data"] == null)
{
Session[ "data" ] = new Dictionary<DateTime, System.Data.DataTable>();
}
return Session[ "data" ] as Dictionary<DateTime, System.Data.DataTable>;
}
set
{
Session[ "data" ] = value;
}
}
Then you can keep your result sets per date
You can use a dictionary of lists also eg:
Dictionary<DateTime, List<YourObject>>
your get QueryFromSession method would be similar to this:
private DataTable GetQueryFromSession( DateTime TheDate)
{
return this.Data[TheDate];
}
or (if you opted for the List<> option)
private List<YourObject> GetQueryFromSession( DateTime TheDate)
{
return this.Data[TheDate];
}
Why not use the Session Name, and create a unique key per dataset based on the dates selected?
What you could do is order the dates in descending order, convert them to strings, and concatenate them to form the unique ID.
Then add the data to the session object using the ID.
精彩评论