Linq Query Query Caching

I am creating a forum package for cms and looking at caching some requests to help with performance, but I'm not sure that caching below will help / do what should be at the bottom (BTW: Cachehelper is a simple helper class that just adds and removes from cache)

          // Set cache variables
        IEnumerable<ForumTopic> maintopics;

        if (!CacheHelper.Get(topicCacheKey, out maintopics))
        {
            // Now get topics
            maintopics = from t in u.ForumTopics
                          where t.ParentNodeId == CurrentNode.Id
                          orderby t.ForumTopicLastPost descending
                          select t;
            // Add to cache
            CacheHelper.Add(maintopics, topicCacheKey);
        }
        //End Cache

        // Pass to my pager helper
        var pagedResults = new PaginatedList<ForumTopic>(maintopics, p ?? 0, Convert.ToInt32(Settings.ForumTopicsPerPage));

        // Now bind
        rptTopicList.DataSource = pagedResults;
        rptTopicList.DataBind();

Is linq only executed when it is enumerated? So it won't work? as its only listing, when I pass it to the paging assistant, which: .Take () a certain number of entries based on the value of querystring 'p'

+3
source share
3 answers

You need to list your results, for example, by calling a method ToList().

maintopics = from t in u.ForumTopics
             where t.ParentNodeId == CurrentNode.Id
             orderby t.ForumTopicLastPost descending
             select t;
// Add to cache
CacheHelper.Add(maintopics.ToList(), topicCacheKey);
+2
source

Linq-to-Sql , , / .

- LoadOptions datacontext. , . , . 10 70 , . ticket- > substatus- > . - L2S , , . ( ) (MSDN ): http://oakleafblog.blogspot.com/2007/08/linq-to-sql-query-execution-with.html

- Linq. . : http://aspguy.wordpress.com/2008/08/15/speed-up-linq-to-sql-with-compiled-linq-queries/

- . , , , .

, , , , ( ASP.NET?), . , SQL-, , , . , "-10 ", , .

0

Source: https://habr.com/ru/post/1789421/


All Articles