<?xml version="1.0"?><?xml-stylesheet type="text/xsl" href="http://code.msdn.microsoft.com/rss.xsl"?><rss version="2.0"><channel><title>Caching LinqToSql results with SqlCacheDependency</title><link>http://code.msdn.microsoft.com/linqtosqlcache/Project/ProjectRss.aspx</link><description>Building a extensionmethod for caching LINQ results with SqlCacheDependency</description><item><title>UPDATED WIKI: Home</title><link>http://code.msdn.microsoft.com/linqtosqlcache/Wiki/View.aspx?title=Home&amp;version=15</link><description>&lt;div class="wikidoc"&gt;
&lt;h2&gt;
What&amp;#180;s this?
&lt;/h2&gt;This page and any other page I may publish on this site contains some code that I&amp;#180;ve written in my projects.&lt;br /&gt;I&amp;#180;ll use these pages as a repository for reusable code, but it&amp;#180;s always fun if someone else can use (or comment, good or bad) the code.&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
Caching Linq results using SqlCacheDependency
&lt;/h2&gt;In early 2008 I needed to cache the results from LinqToSql-queries. I like the SqlCacheDependency (especially in SQL 2K5 Server).&lt;br /&gt;I also like the new extension methods posibillity... So this is what I came up with.&lt;br /&gt; &lt;br /&gt;The code should be explained enough by the comments I think.&lt;br /&gt;&lt;pre&gt;
    public static class Cache
    {
        /// &amp;lt;summary&amp;gt;
        /// Caches Linq query that is created for LinqToSql.
        /// Limits are the same as SqlCacheDependency
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;typeparam name=&amp;quot;T&amp;quot;&amp;gt;&amp;lt;/typeparam&amp;gt;
        /// &amp;lt;param name=&amp;quot;q&amp;quot;&amp;gt;The linq query&amp;lt;/param&amp;gt;
        /// &amp;lt;param name=&amp;quot;dc&amp;quot;&amp;gt;Your LinqToSql DataContext&amp;lt;/param&amp;gt;
        /// &amp;lt;param name=&amp;quot;CacheId&amp;quot;&amp;gt;The unique Id for the cache&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
        public static List&amp;lt;T&amp;gt; LinqCache&amp;lt;T&amp;gt;(this System.Linq.IQueryable&amp;lt;T&amp;gt; q, System.Data.Linq.DataContext dc, string CacheId)
        {
            try
            {
                List&amp;lt;T&amp;gt; objCache = (List&amp;lt;T&amp;gt;)System.Web.HttpRuntime.Cache.Get(CacheId);
 
                if (objCache == null)
                {
                    /////////No cache... implement new SqlCacheDependeny//////////
                    //1. Get connstring from DataContext
                    string connStr = dc.Connection.ConnectionString;
                    //2. Get SqlCommand from DataContext and the LinqQuery
                    string sqlCmd = dc.GetCommand(q).CommandText;                    
                    //3. Create Conn to use in SqlCacheDependency
                    using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr))
                    {
                        conn.Open();
                        //4. Create Command to use in SqlCacheDependency
                        using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sqlCmd, conn))
                        {
                            //5.0 Add all parameters provided by the Linq Query
                            foreach (System.Data.Common.DbParameter dbp in dc.GetCommand(q).Parameters)
                            {
                                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter(dbp.ParameterName, dbp.Value));                                
                            }
                            //5.1 Enable DB for Notifications... Only needed once per DB...
                            System.Web.Caching.SqlCacheDependencyAdmin.EnableNotifications(connStr);
                            //5.2 Get ElementType for the query
                            string NotificationTable = q.ElementType.Name;
                            //5.3 Enable the elementtype for notification (if not done!)
                            if (!System.Web.Caching.SqlCacheDependencyAdmin.GetTablesEnabledForNotifications(connStr).Contains(NotificationTable))
                                System.Web.Caching.SqlCacheDependencyAdmin.EnableTableForNotifications(connStr, NotificationTable);
                            //6. Create SqlCacheDependency
                            System.Web.Caching.SqlCacheDependency sqldep = new System.Web.Caching.SqlCacheDependency(cmd);
                            //7. Refresh the LinqQuery from DB so that we will not use the current Linq cache
                            dc.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, q);
                            //8. Execute SqlCacheDepency query...
                            cmd.ExecuteNonQuery();
                            //9. Execute LINQ-query to have something to cache...
                            objCache = q.ToList();
                            //10. Cache the result but use the already created objectCache. Or else the Linq-query will be executed once more...
                            System.Web.HttpRuntime.Cache.Insert(CacheId, objCache, sqldep);
                        }
                    }
                }
                //Return the created (or cached) List
                return objCache;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
&lt;/pre&gt; &lt;br /&gt;&lt;h3&gt;
IMPORTANT
&lt;/h3&gt;You need to call SqlDependency.Start for example in global.asax&lt;br /&gt;&lt;pre&gt;
   //In Application Start Event
   System.Data.SqlClient.SqlDependency.Start(&amp;quot;DataContextConnectionString&amp;quot;);
   //In Application End Event
   System.Data.SqlClient.SqlDependency.Stop(&amp;quot;DataContextConnectionString&amp;quot;);
&lt;/pre&gt; &lt;br /&gt;&lt;h2&gt;
HowTo: Call the Extension Method
&lt;/h2&gt;So now yo can get call LinqCache&amp;lt;T&amp;gt; on any Linq.IQueryable&amp;lt;T&amp;gt;...&lt;br /&gt;Check the image below for an example from my project.&lt;br /&gt;&lt;a href="javascript:window.location.href='http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqtosqlcache&amp;amp;DownloadId=2528';"&gt;203430687.png&lt;/a&gt;&lt;br /&gt;You can also set some breakpoints in the Extension Method to really see that we collect from the cache until the DB is changed in a way that invalidate our LinqQuery.&lt;br /&gt; &lt;br /&gt;&lt;h5&gt;
Happy Coding
&lt;/h5&gt;&lt;h5&gt;
UTB
&lt;/h5&gt;
&lt;/div&gt;</description><author>UTB</author><pubDate>Wed, 09 Jul 2008 09:00:07 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: Home 20080709A</guid></item><item><title>UPDATED WIKI: Home</title><link>http://code.msdn.microsoft.com/linqtosqlcache/Wiki/View.aspx?title=Home&amp;version=14</link><description>&lt;div class="wikidoc"&gt;
&lt;h2&gt;
What&amp;#180;s this?
&lt;/h2&gt;This page and any other page I may publish on this site contains some code that I&amp;#180;ve written in my projects.&lt;br /&gt;I&amp;#180;ll use these pages as a repository for reusable code, but it&amp;#180;s always fun if someone else can use (or comment, good or bad) the code.&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
Caching Linq results using SqlCacheDependency
&lt;/h2&gt;In early 2008 I needed to cache the results from LinqToSql-queries. I like the SqlCacheDependency (especially in SQL 2K5 Server).&lt;br /&gt;I also like the new extension methods posibillity... So this is what I came up with.&lt;br /&gt; &lt;br /&gt;The code should be explained enough by the comments I think.&lt;br /&gt;&lt;pre&gt;
    public static class Cache
    {
        /// &amp;lt;summary&amp;gt;
        /// Caches Linq query that is created for LinqToSql.
        /// Limits are the same as SqlCacheDependency + some unresolved issues with where statements
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;typeparam name=&amp;quot;T&amp;quot;&amp;gt;&amp;lt;/typeparam&amp;gt;
        /// &amp;lt;param name=&amp;quot;q&amp;quot;&amp;gt;The linq query&amp;lt;/param&amp;gt;
        /// &amp;lt;param name=&amp;quot;dc&amp;quot;&amp;gt;Your LinqToSql DataContext&amp;lt;/param&amp;gt;
        /// &amp;lt;param name=&amp;quot;CacheId&amp;quot;&amp;gt;The unique Id for the cache&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
        public static List&amp;lt;T&amp;gt; LinqCache&amp;lt;T&amp;gt;(this System.Linq.IQueryable&amp;lt;T&amp;gt; q, System.Data.Linq.DataContext dc, string CacheId)
        {
            try
            {
                List&amp;lt;T&amp;gt; objCache = (List&amp;lt;T&amp;gt;)System.Web.HttpRuntime.Cache.Get(CacheId);
 
                if (objCache == null)
                {
                    /////////No cache... implement new SqlCacheDependeny//////////
                    //1. Get connstring from DataContext
                    string connStr = dc.Connection.ConnectionString;
                    //2. Get SqlCommand from DataContext and the LinqQuery
                    string sqlCmd = dc.GetCommand(q).CommandText;                    
                    //3. Create Conn to use in SqlCacheDependency
                    using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr))
                    {
                        conn.Open();
                        //4. Create Command to use in SqlCacheDependency
                        using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sqlCmd, conn))
                        {
                            //5.0 Add all parameters provided by the Linq Query
                            foreach (System.Data.Common.DbParameter dbp in dc.GetCommand(q).Parameters)
                            {
                                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter(dbp.ParameterName, dbp.Value));                                
                            }
                            //5.1 Enable DB for Notifications... Only needed once per DB...
                            System.Web.Caching.SqlCacheDependencyAdmin.EnableNotifications(connStr);
                            //5.2 Get ElementType for the query
                            string NotificationTable = q.ElementType.Name;
                            //5.3 Enable the elementtype for notification (if not done!)
                            if (!System.Web.Caching.SqlCacheDependencyAdmin.GetTablesEnabledForNotifications(connStr).Contains(NotificationTable))
                                System.Web.Caching.SqlCacheDependencyAdmin.EnableTableForNotifications(connStr, NotificationTable);
                            //6. Create SqlCacheDependency
                            System.Web.Caching.SqlCacheDependency sqldep = new System.Web.Caching.SqlCacheDependency(cmd);
                            //7. Refresh the LinqQuery from DB so that we will not use the current Linq cache
                            dc.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, q);
                            //8. Execute SqlCacheDepency query...
                            cmd.ExecuteNonQuery();
                            //9. Execute LINQ-query to have something to cache...
                            objCache = q.ToList();
                            //10. Cache the result but use the already created objectCache. Or else the Linq-query will be executed once more...
                            System.Web.HttpRuntime.Cache.Insert(CacheId, objCache, sqldep);
                        }
                    }
                }
                //Return the created (or cached) List
                return objCache;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
&lt;/pre&gt; &lt;br /&gt;&lt;h3&gt;
IMPORTANT
&lt;/h3&gt;You need to call SqlDependency.Start for example in global.asax&lt;br /&gt;&lt;pre&gt;
   //In Application Start Event
   System.Data.SqlClient.SqlDependency.Start(&amp;quot;DataContextConnectionString&amp;quot;);
   //In Application End Event
   System.Data.SqlClient.SqlDependency.Stop(&amp;quot;DataContextConnectionString&amp;quot;);
&lt;/pre&gt; &lt;br /&gt;&lt;h2&gt;
HowTo: Call the Extension Method
&lt;/h2&gt;So now yo can get call LinqCache&amp;lt;T&amp;gt; on any Linq.IQueryable&amp;lt;T&amp;gt;...&lt;br /&gt;Check the image below for an example from my project.&lt;br /&gt;&lt;a href="javascript:window.location.href='http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqtosqlcache&amp;amp;DownloadId=2528';"&gt;203430687.png&lt;/a&gt;&lt;br /&gt;You can also set some breakpoints in the Extension Method to really see that we collect from the cache until the DB is changed in a way that invalidate our LinqQuery.&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
IMPORTANT ISSUE
&lt;/h2&gt;I&amp;#180;ll need to admit that I&amp;#180;ve not tested this for any purpose other then my own... But while writing this I realised that&lt;br /&gt;you cant have queries with &amp;quot;where&amp;quot; statements. Maybe not a big deal because I do not know a purpose to cache just one or a few objects from a DB.&lt;br /&gt;However you can select the record from the cache filled with List&amp;lt;T&amp;gt; using Linq...&lt;br /&gt; &lt;br /&gt;&lt;h5&gt;
Happy Coding
&lt;/h5&gt;&lt;h5&gt;
UTB
&lt;/h5&gt;
&lt;/div&gt;</description><author>UTB</author><pubDate>Wed, 09 Jul 2008 08:59:17 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: Home 20080709A</guid></item><item><title>UPDATED WIKI: Home</title><link>http://code.msdn.microsoft.com/linqtosqlcache/Wiki/View.aspx?title=Home&amp;version=13</link><description>&lt;div class="wikidoc"&gt;
&lt;h2&gt;
What&amp;#180;s this?
&lt;/h2&gt;This page and any other page I may publish on this site contains some code that I&amp;#180;ve written in my projects.&lt;br /&gt;I&amp;#180;ll use these pages as a repository for reusable code, but it&amp;#180;s always fun if someone else can use (or comment, good or bad) the code.&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
Caching Linq results using SqlCacheDependency
&lt;/h2&gt;In early 2008 I needed to cache the results from LinqToSql-queries. I like the SqlCacheDependency (especially in SQL 2K5 Server).&lt;br /&gt;I also like the new extension methods posibillity... So this is what I came up with.&lt;br /&gt; &lt;br /&gt;The code should be explained enough by the comments I think.&lt;br /&gt;&lt;pre&gt;
    public static class Cache
    {
        /// &amp;lt;summary&amp;gt;
        /// Caches Linq query that is created for LinqToSql.
        /// Limits are the same as SqlCacheDependency + some unresolved issues with where statements
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;typeparam name=&amp;quot;T&amp;quot;&amp;gt;&amp;lt;/typeparam&amp;gt;
        /// &amp;lt;param name=&amp;quot;q&amp;quot;&amp;gt;The linq query&amp;lt;/param&amp;gt;
        /// &amp;lt;param name=&amp;quot;dc&amp;quot;&amp;gt;Your LinqToSql DataContext&amp;lt;/param&amp;gt;
        /// &amp;lt;param name=&amp;quot;CacheId&amp;quot;&amp;gt;The unique Id for the cache&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
        public static List&amp;lt;T&amp;gt; LinqCache&amp;lt;T&amp;gt;(this System.Linq.IQueryable&amp;lt;T&amp;gt; q, System.Data.Linq.DataContext dc, string CacheId)
        {
            try
            {
                List&amp;lt;T&amp;gt; objCache = (List&amp;lt;T&amp;gt;)System.Web.HttpRuntime.Cache.Get(CacheId);
                if (objCache == null)
                {
                    /////////No cache... implement new SqlCacheDependeny//////////
                    //1. Get connstring from DataContext
                    string connStr = dc.Connection.ConnectionString;
                    //2. Get SqlCommand from DataContext and the LinqQuery
                    string sqlCmd = dc.GetCommand(q).CommandText;
                    //3. Create Conn to use in SqlCacheDependency
                    using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr))
                    {
                        conn.Open();
                        //4. Create Command to use in SqlCacheDependency
                        using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sqlCmd, conn))
                        {
                            //5.1 Enable DB for Notifications... Only needed once per DB...
                            System.Web.Caching.SqlCacheDependencyAdmin.EnableNotifications(connStr);
                            //5.2 Get ElementType for the query
                            string NotificationTable = q.ElementType.Name;
                            //5.2 Enable the elementtype for notification (if not done!)
                            if (!System.Web.Caching.SqlCacheDependencyAdmin.GetTablesEnabledForNotifications(connStr).Contains(NotificationTable))
                                System.Web.Caching.SqlCacheDependencyAdmin.EnableTableForNotifications(connStr, NotificationTable);
                            //6. Create SqlCacheDependency
                            System.Web.Caching.SqlCacheDependency sqldep = new System.Web.Caching.SqlCacheDependency(cmd);
                            //7. Refresh the LinqQuery from DB so that we will not use the current Linq cache
                            dc.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, q);
                            //8. Execute SqlCacheDepency query...
                            cmd.ExecuteNonQuery();
                            //9. Execute LINQ-query to have something to cache...
                            objCache = q.ToList();
                            //10. Cache the result but use the already created objectCache. Or else the Linq-query will be executed once more...
                            System.Web.HttpRuntime.Cache.Insert(CacheId, objCache, sqldep);
                        }
                    }
                }
                //Return the created (or cached) List
                return objCache;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
&lt;/pre&gt; &lt;br /&gt;&lt;h3&gt;
IMPORTANT
&lt;/h3&gt;You need to call SqlDependency.Start for example in global.asax&lt;br /&gt;&lt;pre&gt;
   //In Application Start Event
   System.Data.SqlClient.SqlDependency.Start(&amp;quot;DataContextConnectionString&amp;quot;);
   //In Application End Event
   System.Data.SqlClient.SqlDependency.Stop(&amp;quot;DataContextConnectionString&amp;quot;);
&lt;/pre&gt; &lt;br /&gt;&lt;h2&gt;
HowTo: Call the Extension Method
&lt;/h2&gt;So now yo can get call LinqCache&amp;lt;T&amp;gt; on any Linq.IQueryable&amp;lt;T&amp;gt;...&lt;br /&gt;Check the image below for an example from my project.&lt;br /&gt;&lt;a href="javascript:window.location.href='http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqtosqlcache&amp;amp;DownloadId=2528';"&gt;203430687.png&lt;/a&gt;&lt;br /&gt;You can also set some breakpoints in the Extension Method to really see that we collect from the cache until the DB is changed in a way that invalidate our LinqQuery.&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
IMPORTANT ISSUE
&lt;/h2&gt;I&amp;#180;ll need to admit that I&amp;#180;ve not tested this for any purpose other then my own... But while writing this I realised that&lt;br /&gt;you cant have queries with &amp;quot;where&amp;quot; statements. Maybe not a big deal because I do not know a purpose to cache just one or a few objects from a DB.&lt;br /&gt;However you can select the record from the cache filled with List&amp;lt;T&amp;gt; using Linq...&lt;br /&gt; &lt;br /&gt;&lt;h5&gt;
Happy Coding
&lt;/h5&gt;&lt;h5&gt;
UTB
&lt;/h5&gt;
&lt;/div&gt;</description><author>UTB</author><pubDate>Tue, 08 Jul 2008 19:29:26 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: Home 20080708P</guid></item><item><title>UPDATED WIKI: Home</title><link>http://code.msdn.microsoft.com/linqtosqlcache/Wiki/View.aspx?title=Home&amp;version=12</link><description>&lt;div class="wikidoc"&gt;
&lt;h2&gt;
What&amp;#180;s this?
&lt;/h2&gt;This page and any other page I may publish on this site contains some code that I&amp;#180;ve written in my projects.&lt;br /&gt;I&amp;#180;ll use these pages as a repository for reusable code, but it&amp;#180;s always fun if someone else can use (or comment, good or bad) the code.&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
Caching Linq results using SqlCacheDependency
&lt;/h2&gt;In early 2008 I needed to cache the results from LinqToSql-queries. I like the SqlCacheDependency (especially in SQL 2K5 Server).&lt;br /&gt;I also like the new extension methods posibillity... So this is what I came up with.&lt;br /&gt; &lt;br /&gt;The code should be explained enough by the comments I think.&lt;br /&gt;&lt;pre&gt;
    public static class Cache
    {
        /// &amp;lt;summary&amp;gt;
        /// Caches Linq query that is created for LinqToSql.
        /// Limits are the same as SqlCacheDependency + some unresolved issues with where statements
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;typeparam name=&amp;quot;T&amp;quot;&amp;gt;&amp;lt;/typeparam&amp;gt;
        /// &amp;lt;param name=&amp;quot;q&amp;quot;&amp;gt;The linq query&amp;lt;/param&amp;gt;
        /// &amp;lt;param name=&amp;quot;dc&amp;quot;&amp;gt;Your LinqToSql DataContext&amp;lt;/param&amp;gt;
        /// &amp;lt;param name=&amp;quot;CacheId&amp;quot;&amp;gt;The unique Id for the cache&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
        public static List&amp;lt;T&amp;gt; LinqCache&amp;lt;T&amp;gt;(this System.Linq.IQueryable&amp;lt;T&amp;gt; q, System.Data.Linq.DataContext dc, string CacheId)
        {
            try
            {
                List&amp;lt;T&amp;gt; objCache = (List&amp;lt;T&amp;gt;)System.Web.HttpRuntime.Cache.Get(CacheId);
                if (objCache == null)
                {
                    /////////No cache... implement new SqlCacheDependeny//////////
                    //1. Get connstring from DataContext
                    string connStr = dc.Connection.ConnectionString;
                    //2. Get SqlCommand from DataContext and the LinqQuery
                    string sqlCmd = dc.GetCommand(q).CommandText;
                    //3. Create Conn to use in SqlCacheDependency
                    using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr))
                    {
                        conn.Open();
                        //4. Create Command to use in SqlCacheDependency
                        using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sqlCmd, conn))
                        {
                            //5.1 Enable DB for Notifications... Only needed once per DB...
                            System.Web.Caching.SqlCacheDependencyAdmin.EnableNotifications(connStr);
                            //5.2 Get ElementType for the query
                            string NotificationTable = q.ElementType.Name;
                            //5.2 Enable the elementtype for notification (if not done!)
                            if (!System.Web.Caching.SqlCacheDependencyAdmin.GetTablesEnabledForNotifications(connStr).Contains(NotificationTable))
                                System.Web.Caching.SqlCacheDependencyAdmin.EnableTableForNotifications(connStr, NotificationTable);
                            //6. Create SqlCacheDependency
                            System.Web.Caching.SqlCacheDependency sqldep = new System.Web.Caching.SqlCacheDependency(cmd);
                            //7. Refresh the LinqQuery from DB so that we will not use the current Linq cache
                            dc.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, q);
                            //8. Execute SqlCacheDepency query...
                            cmd.ExecuteNonQuery();
                            //9. Execute LINQ-query to have something to cache...
                            objCache = q.ToList();
                            //10. Cache the result but use the already created objectCache. Or else the Linq-query will be executed once more...
                            System.Web.HttpRuntime.Cache.Insert(CacheId, objCache, sqldep);
                        }
                    }
                }
                //Return the created (or cached) List
                return objCache;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
&lt;/pre&gt; &lt;br /&gt;&lt;h3&gt;
IMPORTANT
&lt;/h3&gt;You need to call SqlDependency.Start for example in global.asax&lt;br /&gt;&lt;pre&gt;
   //In Application Start Event
   System.Data.SqlClient.SqlDependency.Start(&amp;quot;DataContextConnectionString&amp;quot;);
   //In Application End Event
   System.Data.SqlClient.SqlDependency.Stop(&amp;quot;DataContextConnectionString&amp;quot;);
&lt;/pre&gt; &lt;br /&gt;&lt;h2&gt;
HowTo: Call the Extension Method
&lt;/h2&gt;So now yo can get call LinqCache&amp;lt;T&amp;gt; on any Linq.IQueryable&amp;lt;T&amp;gt;...&lt;br /&gt;Check the image below for an example from my project.&lt;br /&gt;&lt;a href="javascript:window.location.href='http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqtosqlcache&amp;amp;DownloadId=2528';"&gt;203430687.png&lt;/a&gt;&lt;br /&gt;You can also set some breakpoints in the Extension Method to really see that we collect from the cache until the DB is changed in a way that invalidate our LinqQuery.&lt;br /&gt; &lt;br /&gt;&lt;h1&gt;
IMPORTANT ISSUE
&lt;/h1&gt;I&amp;#180;ll need to admit that I&amp;#180;ve not tested this for any purpose other then my own... But while writing this I realised that&lt;br /&gt;you cant have queries with &amp;quot;where&amp;quot; statements. Maybe not a big deal because I do not know a purpose to cache just one or a few objects from a DB.&lt;br /&gt;However you can select the record from the cache filled with List&amp;lt;T&amp;gt; using Linq...&lt;br /&gt; &lt;br /&gt;&lt;h5&gt;
Happy Coding
&lt;/h5&gt;&lt;h5&gt;
UTB
&lt;/h5&gt;
&lt;/div&gt;</description><author>UTB</author><pubDate>Tue, 08 Jul 2008 19:25:57 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: Home 20080708P</guid></item><item><title>UPDATED WIKI: Home</title><link>http://code.msdn.microsoft.com/linqtosqlcache/Wiki/View.aspx?title=Home&amp;version=11</link><description>&lt;div class="wikidoc"&gt;
&lt;h2&gt;
What&amp;#180;s this?
&lt;/h2&gt;This page and any other page I may publish on this site contains some code that I&amp;#180;ve written in my projects.&lt;br /&gt;I&amp;#180;ll use these pages as a repository for reusable code, but it&amp;#180;s always fun if someone else can use (or comment, good or bad) the code.&lt;br /&gt; &lt;br /&gt;&lt;h1&gt;
IMPORTANT ISSUE
&lt;/h1&gt;I&amp;#180;ll need to admit that I&amp;#180;ve not tested this for any purpose other then my own... But while writing this I realised that&lt;br /&gt;you cant have queris with where statements. Maybe not a big deal because I always cache everything and not just one record.&lt;br /&gt;With linq you can select the record from the cache using Linq&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
Caching Linq results using SqlCacheDependency
&lt;/h2&gt;In early 2008 I needed to cache the results from LinqToSql-queries. I like the SqlCacheDependency (especially in SQL 2K5 Server).&lt;br /&gt;I also like the new extension methods posibillity... So this is what I came up with.&lt;br /&gt; &lt;br /&gt;The code should be explained enough by the comments I think.&lt;br /&gt;&lt;pre&gt;
    public static class Cache
    {
        /// &amp;lt;summary&amp;gt;
        /// Caches Linq query that is created for LinqToSql.
        /// Limits are the same as SqlCacheDependency + some unresolved issues with where statements
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;typeparam name=&amp;quot;T&amp;quot;&amp;gt;&amp;lt;/typeparam&amp;gt;
        /// &amp;lt;param name=&amp;quot;q&amp;quot;&amp;gt;The linq query&amp;lt;/param&amp;gt;
        /// &amp;lt;param name=&amp;quot;dc&amp;quot;&amp;gt;Your LinqToSql DataContext&amp;lt;/param&amp;gt;
        /// &amp;lt;param name=&amp;quot;CacheId&amp;quot;&amp;gt;The unique Id for the cache&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
        public static List&amp;lt;T&amp;gt; LinqCache&amp;lt;T&amp;gt;(this System.Linq.IQueryable&amp;lt;T&amp;gt; q, System.Data.Linq.DataContext dc, string CacheId)
        {
            try
            {
                List&amp;lt;T&amp;gt; objCache = (List&amp;lt;T&amp;gt;)System.Web.HttpRuntime.Cache.Get(CacheId);
                if (objCache == null)
                {
                    /////////No cache... implement new SqlCacheDependeny//////////
                    //1. Get connstring from DataContext
                    string connStr = dc.Connection.ConnectionString;
                    //2. Get SqlCommand from DataContext and the LinqQuery
                    string sqlCmd = dc.GetCommand(q).CommandText;
                    //3. Create Conn to use in SqlCacheDependency
                    using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr))
                    {
                        conn.Open();
                        //4. Create Command to use in SqlCacheDependency
                        using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sqlCmd, conn))
                        {
                            //5.1 Enable DB for Notifications... Only needed once per DB...
                            System.Web.Caching.SqlCacheDependencyAdmin.EnableNotifications(connStr);
                            //5.2 Get ElementType for the query
                            string NotificationTable = q.ElementType.Name;
                            //5.2 Enable the elementtype for notification (if not done!)
                            if (!System.Web.Caching.SqlCacheDependencyAdmin.GetTablesEnabledForNotifications(connStr).Contains(NotificationTable))
                                System.Web.Caching.SqlCacheDependencyAdmin.EnableTableForNotifications(connStr, NotificationTable);
                            //6. Create SqlCacheDependency
                            System.Web.Caching.SqlCacheDependency sqldep = new System.Web.Caching.SqlCacheDependency(cmd);
                            //7. Refresh the LinqQuery from DB so that we will not use the current Linq cache
                            dc.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, q);
                            //8. Execute SqlCacheDepency query...
                            cmd.ExecuteNonQuery();
                            //9. Execute LINQ-query to have something to cache...
                            objCache = q.ToList();
                            //10. Cache the result but use the already created objectCache. Or else the Linq-query will be executed once more...
                            System.Web.HttpRuntime.Cache.Insert(CacheId, objCache, sqldep);
                        }
                    }
                }
                //Return the created (or cached) List
                return objCache;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
&lt;/pre&gt; &lt;br /&gt;&lt;h3&gt;
IMPORTANT
&lt;/h3&gt;You need to call SqlDependency.Start for example in global.asax&lt;br /&gt;&lt;pre&gt;
   //In Application Start Event
   System.Data.SqlClient.SqlDependency.Start(&amp;quot;DataContextConnectionString&amp;quot;);
   //In Application End Event
   System.Data.SqlClient.SqlDependency.Stop(&amp;quot;DataContextConnectionString&amp;quot;);
&lt;/pre&gt; &lt;br /&gt;&lt;h2&gt;
HowTo: Call the Extension Method
&lt;/h2&gt;So now yo can get call LinqCache&amp;lt;T&amp;gt; on any Linq.IQueryable&amp;lt;T&amp;gt;...&lt;br /&gt;Check the image below for an example from my project.&lt;br /&gt;&lt;a href="javascript:window.location.href='http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqtosqlcache&amp;amp;DownloadId=2528';"&gt;203430687.png&lt;/a&gt;&lt;br /&gt;You can also set some breakpoints in the Extension Method to really see that we collect from the cache until the DB is changed in a way that invalidate our LinqQuery.&lt;br /&gt; &lt;br /&gt;&lt;h5&gt;
Happy Coding
&lt;/h5&gt;&lt;h5&gt;
UTB
&lt;/h5&gt;
&lt;/div&gt;</description><author>UTB</author><pubDate>Tue, 08 Jul 2008 19:23:19 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: Home 20080708P</guid></item><item><title>UPDATED WIKI: Home</title><link>http://code.msdn.microsoft.com/linqtosqlcache/Wiki/View.aspx?title=Home&amp;version=10</link><description>&lt;div class="wikidoc"&gt;
&lt;h2&gt;
What&amp;#180;s this?
&lt;/h2&gt;This page and any other page I may publish on this site contains some code that I&amp;#180;ve written in my projects.&lt;br /&gt;I&amp;#180;ll use these pages as a repository for reusable code, bur it&amp;#180;s always fun if someone else can use (or comment, good or bad) the code.&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
Caching Linq results using SqlCacheDependency
&lt;/h2&gt;In early 2008 I needed to cache the results from LinqToSql-queries. I like the SqlCacheDependency (especially in SQL 2K5 Server).&lt;br /&gt;I also like the new extension methods posibillity... So this is what I came up with.&lt;br /&gt; &lt;br /&gt;The code should be explained enough by the comments I think.&lt;br /&gt;&lt;pre&gt;
    public static class Cache
    {
        /// &amp;lt;summary&amp;gt;
        /// Caches any Linq query that is created for LinqToSql.
        /// Limits are the same as SqlCacheDependency
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;typeparam name=&amp;quot;T&amp;quot;&amp;gt;&amp;lt;/typeparam&amp;gt;
        /// &amp;lt;param name=&amp;quot;q&amp;quot;&amp;gt;The linq query&amp;lt;/param&amp;gt;
        /// &amp;lt;param name=&amp;quot;dc&amp;quot;&amp;gt;Your LinqToSql DataContext&amp;lt;/param&amp;gt;
        /// &amp;lt;param name=&amp;quot;CacheId&amp;quot;&amp;gt;The unique Id for the cache&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
        public static List&amp;lt;T&amp;gt; LinqCache&amp;lt;T&amp;gt;(this System.Linq.IQueryable&amp;lt;T&amp;gt; q, System.Data.Linq.DataContext dc, string CacheId)
        {
            try
            {
                List&amp;lt;T&amp;gt; objCache = (List&amp;lt;T&amp;gt;)System.Web.HttpRuntime.Cache.Get(CacheId);
                if (objCache == null)
                {
                    /////////No cache... implement new SqlCacheDependeny//////////
                    //1. Get connstring from DataContext
                    string connStr = dc.Connection.ConnectionString;
                    //2. Get SqlCommand from DataContext and the LinqQuery
                    string sqlCmd = dc.GetCommand(q).CommandText;
                    //3. Create Conn to use in SqlCacheDependency
                    using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr))
                    {
                        conn.Open();
                        //4. Create Command to use in SqlCacheDependency
                        using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sqlCmd, conn))
                        {
                            //5.1 Enable DB for Notifications... Only needed once per DB...
                            System.Web.Caching.SqlCacheDependencyAdmin.EnableNotifications(connStr);
                            //5.2 Get ElementType for the query
                            string NotificationTable = q.ElementType.Name;
                            //5.2 Enable the elementtype for notification (if not done!)
                            if (!System.Web.Caching.SqlCacheDependencyAdmin.GetTablesEnabledForNotifications(connStr).Contains(NotificationTable))
                                System.Web.Caching.SqlCacheDependencyAdmin.EnableTableForNotifications(connStr, NotificationTable);
                            //6. Create SqlCacheDependency
                            System.Web.Caching.SqlCacheDependency sqldep = new System.Web.Caching.SqlCacheDependency(cmd);
                            //7. Refresh the LinqQuery from DB so that we will not use the current Linq cache
                            dc.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, q);
                            //8. Execute SqlCacheDepency query...
                            cmd.ExecuteNonQuery();
                            //9. Execute LINQ-query to have something to cache...
                            objCache = q.ToList();
                            //10. Cache the result but use the already created objectCache. Or else the Linq-query will be executed once more...
                            System.Web.HttpRuntime.Cache.Insert(CacheId, objCache, sqldep);
                        }
                    }
                }
                //Return the created (or cached) List
                return objCache;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
&lt;/pre&gt; &lt;br /&gt;&lt;h3&gt;
IMPORTANT
&lt;/h3&gt;You need to call SqlDependency.Start for example in global.asax&lt;br /&gt;&lt;pre&gt;
   //In Application Start Event
   System.Data.SqlClient.SqlDependency.Start(&amp;quot;DataContextConnectionString&amp;quot;);
   //In Application End Event
   System.Data.SqlClient.SqlDependency.Stop(&amp;quot;DataContextConnectionString&amp;quot;);
&lt;/pre&gt; &lt;br /&gt;&lt;h2&gt;
Calling the Extension Method
&lt;/h2&gt;So now yo can get call LinqCache&amp;lt;T&amp;gt; on any Linq.IQueryable&amp;lt;T&amp;gt;...&lt;br /&gt;Check the image below for an example from my project.&lt;br /&gt;&lt;a href="javascript:window.location.href='http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqtosqlcache&amp;amp;DownloadId=2528';"&gt;203430687.png&lt;/a&gt;&lt;br /&gt;You can also set some breakpoints in the Extension Method to really see that we collect from the cache until the DB is changed in a way that invalidate our LinqQuery.&lt;br /&gt; &lt;br /&gt;&lt;h5&gt;
Happy Coding
&lt;/h5&gt;&lt;h5&gt;
UTB
&lt;/h5&gt;
&lt;/div&gt;</description><author>UTB</author><pubDate>Tue, 08 Jul 2008 18:58:29 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: Home 20080708P</guid></item><item><title>UPDATED WIKI: Home</title><link>http://code.msdn.microsoft.com/linqtosqlcache/Wiki/View.aspx?title=Home&amp;version=9</link><description>&lt;div class="wikidoc"&gt;
&lt;b&gt;Resource Page Description&lt;/b&gt;&lt;br /&gt;Building a extensionmethod for caching LINQ results with SqlCacheDependency
&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
What&amp;#180;s this?
&lt;/h2&gt;This page and any other page I may publish on this site contains some code that I&amp;#180;ve written in my projects.&lt;br /&gt;I&amp;#180;ll use these pages as a repository for reusable code, bur it&amp;#180;s always fun if someone else can use (or comment, good or bad) the code.&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
Caching Linq results using SqlCacheDependency
&lt;/h2&gt;In early 2008 I needed to cache the results from LinqToSql-queries. I like the SqlCacheDependency (especially in SQL 2K5 Server).&lt;br /&gt;I also like the new extension methods posibillity... So this is what I came up with.&lt;br /&gt; &lt;br /&gt;The code should be explained enough by the comments I think.&lt;br /&gt;&lt;pre&gt;
    public static class Cache
    {
        /// &amp;lt;summary&amp;gt;
        /// Caches any Linq query that is created for LinqToSql.
        /// Limits are the same as SqlCacheDependency
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;typeparam name=&amp;quot;T&amp;quot;&amp;gt;&amp;lt;/typeparam&amp;gt;
        /// &amp;lt;param name=&amp;quot;q&amp;quot;&amp;gt;The linq query&amp;lt;/param&amp;gt;
        /// &amp;lt;param name=&amp;quot;dc&amp;quot;&amp;gt;Your LinqToSql DataContext&amp;lt;/param&amp;gt;
        /// &amp;lt;param name=&amp;quot;CacheId&amp;quot;&amp;gt;The unique Id for the cache&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
        public static List&amp;lt;T&amp;gt; LinqCache&amp;lt;T&amp;gt;(this System.Linq.IQueryable&amp;lt;T&amp;gt; q, System.Data.Linq.DataContext dc, string CacheId)
        {
            try
            {
                List&amp;lt;T&amp;gt; objCache = (List&amp;lt;T&amp;gt;)System.Web.HttpRuntime.Cache.Get(CacheId);
                if (objCache == null)
                {
                    /////////No cache... implement new SqlCacheDependeny//////////
                    //1. Get connstring from DataContext
                    string connStr = dc.Connection.ConnectionString;
                    //2. Get SqlCommand from DataContext and the LinqQuery
                    string sqlCmd = dc.GetCommand(q).CommandText;
                    //3. Create Conn to use in SqlCacheDependency
                    using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr))
                    {
                        conn.Open();
                        //4. Create Command to use in SqlCacheDependency
                        using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sqlCmd, conn))
                        {
                            //5.1 Enable DB for Notifications... Only needed once per DB...
                            System.Web.Caching.SqlCacheDependencyAdmin.EnableNotifications(connStr);
                            //5.2 Get ElementType for the query
                            string NotificationTable = q.ElementType.Name;
                            //5.2 Enable the elementtype for notification (if not done!)
                            if (!System.Web.Caching.SqlCacheDependencyAdmin.GetTablesEnabledForNotifications(connStr).Contains(NotificationTable))
                                System.Web.Caching.SqlCacheDependencyAdmin.EnableTableForNotifications(connStr, NotificationTable);
                            //6. Create SqlCacheDependency
                            System.Web.Caching.SqlCacheDependency sqldep = new System.Web.Caching.SqlCacheDependency(cmd);
                            //7. Refresh the LinqQuery from DB so that we will not use the current Linq cache
                            dc.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, q);
                            //8. Execute SqlCacheDepency query...
                            cmd.ExecuteNonQuery();
                            //9. Execute LINQ-query to have something to cache...
                            objCache = q.ToList();
                            //10. Cache the result but use the already created objectCache. Or else the Linq-query will be executed once more...
                            System.Web.HttpRuntime.Cache.Insert(CacheId, objCache, sqldep);
                        }
                    }
                }
                //Return the created (or cached) List
                return objCache;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
&lt;/pre&gt; &lt;br /&gt;&lt;h3&gt;
IMPORTANT
&lt;/h3&gt;You need to call SqlDependency.Start for example in global.asax&lt;br /&gt;&lt;pre&gt;
   //In Application Start Event
   System.Data.SqlClient.SqlDependency.Start(&amp;quot;DataContextConnectionString&amp;quot;);
   //In Application End Event
   System.Data.SqlClient.SqlDependency.Stop(&amp;quot;DataContextConnectionString&amp;quot;);
&lt;/pre&gt; &lt;br /&gt;&lt;h2&gt;
Calling the Extension Method
&lt;/h2&gt;So now yo can get call LinqCache&amp;lt;T&amp;gt; on any Linq.IQueryable&amp;lt;T&amp;gt;...&lt;br /&gt;Check the image below for an example from my project.&lt;br /&gt;&lt;a href="javascript:window.location.href='http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqtosqlcache&amp;amp;DownloadId=2528';"&gt;203430687.png&lt;/a&gt;&lt;br /&gt;You can also set some breakpoints in extension method to really see that we collect from the cache until a row is changed in the DB that would invalidate our LinqQuery.&lt;br /&gt; &lt;br /&gt;&lt;b&gt;**Delete the following note before publishing **&lt;/b&gt;&lt;br /&gt; &lt;br /&gt;This resource page is currently in setup mode and only available to coordinators and developers. Once you have finished setting up your resource page you can publish it to make it available to all MSDN Code Gallery visitors.&lt;br /&gt; &lt;br /&gt;To get your Resource Page ready to publish, you should do the following:&lt;br /&gt;&lt;ol&gt;
&lt;li&gt;Make any changes to the details of your resource page&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;Here you can enable or disable functions of your resource page.  You might want to turn on the Issue Tracker to allow users to provide feedback on your resource, or if you have a resource that does not involve a code sample, you may want to turn off the Releases tab.&lt;/li&gt;&lt;li&gt;Make sure your resource page description is detailed enough to let people search for your resource.&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;Add your code sample or other resources to the resource page&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;If you’re uploading code, go to the Releases tab and create a new release to house your code.  Creating a release allows you to have the license properly displayed when people download your code, as well as provides a download count.&lt;/li&gt;&lt;li&gt;Edit your Wiki page to attach any resources you may have that are not source code.&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;If you want to let someone see your resource page before it is published, go to the People tab and add them to your resource page&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;This will let you add other team members who may be contributing to your resource, or just show it off and get feedback from someone you trust.&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;Tag your resource page with descriptive tags to make it easier for people to find your resources when browsing the gallery.&lt;/li&gt;&lt;li&gt;Publish your resource page so it becomes visible to everyone!&lt;/li&gt;
&lt;/ol&gt; &lt;br /&gt;Additional information on starting a new resource page is available here: &lt;a href="http://code.msdn.microsoft.com/CodeGallery" class="externalLink"&gt;Resource Page Startup Guide&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;.&lt;br /&gt;
&lt;/div&gt;</description><author>UTB</author><pubDate>Tue, 08 Jul 2008 18:55:53 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: Home 20080708P</guid></item><item><title>UPDATED WIKI: Home</title><link>http://code.msdn.microsoft.com/linqtosqlcache/Wiki/View.aspx?title=Home&amp;version=8</link><description>&lt;div class="wikidoc"&gt;
&lt;h2&gt;
What&amp;#180;s this?
&lt;/h2&gt;This page and any other page I may publish on this site contains some code that I&amp;#180;ve written in my projects.&lt;br /&gt;I&amp;#180;ll use these pages as a repository for reusable code, bur it&amp;#180;s always fun if someone else can use (or comment, good or bad) the code.&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
Caching Linq results using SqlCacheDependency
&lt;/h2&gt;In early 2008 I needed to cache the results from LinqToSql-queries. I like the SqlCacheDependency (especially in SQL 2K5 Server).&lt;br /&gt;I also like the new extension methods posibillity... So this is what I came up with.&lt;br /&gt; &lt;br /&gt;The code should be explained enough by the comments I think.&lt;br /&gt;&lt;pre&gt;
    public static class Cache
    {
        /// &amp;lt;summary&amp;gt;
        /// Caches any Linq query that is created for LinqToSql.
        /// Limits are the same as SqlCacheDependency
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;typeparam name=&amp;quot;T&amp;quot;&amp;gt;&amp;lt;/typeparam&amp;gt;
        /// &amp;lt;param name=&amp;quot;q&amp;quot;&amp;gt;The linq query&amp;lt;/param&amp;gt;
        /// &amp;lt;param name=&amp;quot;dc&amp;quot;&amp;gt;Your LinqToSql DataContext&amp;lt;/param&amp;gt;
        /// &amp;lt;param name=&amp;quot;CacheId&amp;quot;&amp;gt;The unique Id for the cache&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
        public static List&amp;lt;T&amp;gt; LinqCache&amp;lt;T&amp;gt;(this System.Linq.IQueryable&amp;lt;T&amp;gt; q, System.Data.Linq.DataContext dc, string CacheId)
        {
            try
            {
                List&amp;lt;T&amp;gt; objCache = (List&amp;lt;T&amp;gt;)System.Web.HttpRuntime.Cache.Get(CacheId);
                if (objCache == null)
                {
                    /////////No cache... implement new SqlCacheDependeny//////////
                    //1. Get connstring from DataContext
                    string connStr = dc.Connection.ConnectionString;
                    //2. Get SqlCommand from DataContext and the LinqQuery
                    string sqlCmd = dc.GetCommand(q).CommandText;
                    //3. Create Conn to use in SqlCacheDependency
                    using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr))
                    {
                        conn.Open();
                        //4. Create Command to use in SqlCacheDependency
                        using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sqlCmd, conn))
                        {
                            //5.1 Enable DB for Notifications... Only needed once per DB...
                            System.Web.Caching.SqlCacheDependencyAdmin.EnableNotifications(connStr);
                            //5.2 Get ElementType for the query
                            string NotificationTable = q.ElementType.Name;
                            //5.2 Enable the elementtype for notification (if not done!)
                            if (!System.Web.Caching.SqlCacheDependencyAdmin.GetTablesEnabledForNotifications(connStr).Contains(NotificationTable))
                                System.Web.Caching.SqlCacheDependencyAdmin.EnableTableForNotifications(connStr, NotificationTable);
                            //6. Create SqlCacheDependency
                            System.Web.Caching.SqlCacheDependency sqldep = new System.Web.Caching.SqlCacheDependency(cmd);
                            //7. Refresh the LinqQuery from DB so that we will not use the current Linq cache
                            dc.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, q);
                            //8. Execute SqlCacheDepency query...
                            cmd.ExecuteNonQuery();
                            //9. Execute LINQ-query to have something to cache...
                            objCache = q.ToList();
                            //10. Cache the result but use the already created objectCache. Or else the Linq-query will be executed once more...
                            System.Web.HttpRuntime.Cache.Insert(CacheId, objCache, sqldep);
                        }
                    }
                }
                //Return the created (or cached) List
                return objCache;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
&lt;/pre&gt; &lt;br /&gt;&lt;h3&gt;
IMPORTANT
&lt;/h3&gt;You need to call SqlDependency.Start for example in global.asax&lt;br /&gt;&lt;pre&gt;
   //In Application Start Event
   System.Data.SqlClient.SqlDependency.Start(&amp;quot;DataContextConnectionString&amp;quot;);
   //In Application End Event
   System.Data.SqlClient.SqlDependency.Stop(&amp;quot;DataContextConnectionString&amp;quot;);
&lt;/pre&gt; &lt;br /&gt;&lt;h2&gt;
Calling the Extension Method
&lt;/h2&gt;&lt;a href="javascript:window.location.href='http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqtosqlcache&amp;amp;DownloadId=2528';"&gt;203430687.png&lt;/a&gt;&lt;br /&gt;&lt;b&gt;Resource Page Description&lt;/b&gt;&lt;br /&gt;Building a extensionmethod for caching LINQ results with SqlCacheDependency
&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&lt;b&gt;**Delete the following note before publishing **&lt;/b&gt;&lt;br /&gt; &lt;br /&gt;This resource page is currently in setup mode and only available to coordinators and developers. Once you have finished setting up your resource page you can publish it to make it available to all MSDN Code Gallery visitors.&lt;br /&gt; &lt;br /&gt;To get your Resource Page ready to publish, you should do the following:&lt;br /&gt;&lt;ol&gt;
&lt;li&gt;Make any changes to the details of your resource page&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;Here you can enable or disable functions of your resource page.  You might want to turn on the Issue Tracker to allow users to provide feedback on your resource, or if you have a resource that does not involve a code sample, you may want to turn off the Releases tab.&lt;/li&gt;&lt;li&gt;Make sure your resource page description is detailed enough to let people search for your resource.&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;Add your code sample or other resources to the resource page&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;If you’re uploading code, go to the Releases tab and create a new release to house your code.  Creating a release allows you to have the license properly displayed when people download your code, as well as provides a download count.&lt;/li&gt;&lt;li&gt;Edit your Wiki page to attach any resources you may have that are not source code.&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;If you want to let someone see your resource page before it is published, go to the People tab and add them to your resource page&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;This will let you add other team members who may be contributing to your resource, or just show it off and get feedback from someone you trust.&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;Tag your resource page with descriptive tags to make it easier for people to find your resources when browsing the gallery.&lt;/li&gt;&lt;li&gt;Publish your resource page so it becomes visible to everyone!&lt;/li&gt;
&lt;/ol&gt; &lt;br /&gt;Additional information on starting a new resource page is available here: &lt;a href="http://code.msdn.microsoft.com/CodeGallery" class="externalLink"&gt;Resource Page Startup Guide&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;.&lt;br /&gt;
&lt;/div&gt;</description><author>UTB</author><pubDate>Tue, 08 Jul 2008 18:51:17 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: Home 20080708P</guid></item><item><title>UPDATED WIKI: Home</title><link>http://code.msdn.microsoft.com/linqtosqlcache/Wiki/View.aspx?title=Home&amp;version=7</link><description>&lt;div class="wikidoc"&gt;
&lt;h2&gt;
What&amp;#180;s this?
&lt;/h2&gt;This page and any other page I may publish on this site contains some code that I&amp;#180;ve written in my projects.&lt;br /&gt;I&amp;#180;ll use these pages as a repository for reusable code, bur it&amp;#180;s always fun if someone else can use (or comment, good or bad) the code.&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
Caching Linq results using SqlCacheDependency
&lt;/h2&gt;In early 2008 I needed to cache the results from LinqToSql-queries. I like the SqlCacheDependency (especially in SQL 2K5 Server).&lt;br /&gt;I also like the new extension methods posibillity... So this is what I came up with.&lt;br /&gt; &lt;br /&gt;The code should be explained enough by the comments I think.&lt;br /&gt;&lt;pre&gt;
    public static class Cache
    {
        /// &amp;lt;summary&amp;gt;
        /// Caches any Linq query that is created for LinqToSql.
        /// Limits are the same as SqlCacheDependency
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;typeparam name=&amp;quot;T&amp;quot;&amp;gt;&amp;lt;/typeparam&amp;gt;
        /// &amp;lt;param name=&amp;quot;q&amp;quot;&amp;gt;The linq query&amp;lt;/param&amp;gt;
        /// &amp;lt;param name=&amp;quot;dc&amp;quot;&amp;gt;Your LinqToSql DataContext&amp;lt;/param&amp;gt;
        /// &amp;lt;param name=&amp;quot;CacheId&amp;quot;&amp;gt;The unique Id for the cache&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
        public static List&amp;lt;T&amp;gt; LinqCache&amp;lt;T&amp;gt;(this System.Linq.IQueryable&amp;lt;T&amp;gt; q, System.Data.Linq.DataContext dc, string CacheId)
        {
            try
            {
                List&amp;lt;T&amp;gt; objCache = (List&amp;lt;T&amp;gt;)System.Web.HttpRuntime.Cache.Get(CacheId);
                if (objCache == null)
                {
                    /////////No cache... implement new SqlCacheDependeny//////////
                    //1. Get connstring from DataContext
                    string connStr = dc.Connection.ConnectionString;
                    //2. Get SqlCommand from DataContext and the LinqQuery
                    string sqlCmd = dc.GetCommand(q).CommandText;
                    //3. Create Conn to use in SqlCacheDependency
                    using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr))
                    {
                        conn.Open();
                        //4. Create Command to use in SqlCacheDependency
                        using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sqlCmd, conn))
                        {
                            //5.1 Enable DB for Notifications... Only needed once per DB...
                            System.Web.Caching.SqlCacheDependencyAdmin.EnableNotifications(connStr);
                            //5.2 Get ElementType for the query
                            string NotificationTable = q.ElementType.Name;
                            //5.2 Enable the elementtype for notification (if not done!)
                            if (!System.Web.Caching.SqlCacheDependencyAdmin.GetTablesEnabledForNotifications(connStr).Contains(NotificationTable))
                                System.Web.Caching.SqlCacheDependencyAdmin.EnableTableForNotifications(connStr, NotificationTable);
                            //6. Create SqlCacheDependency
                            System.Web.Caching.SqlCacheDependency sqldep = new System.Web.Caching.SqlCacheDependency(cmd);
                            //7. Refresh the LinqQuery from DB so that we will not use the current Linq cache
                            dc.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, q);
                            //8. Execute SqlCacheDepency query...
                            cmd.ExecuteNonQuery();
                            //9. Execute LINQ-query to have something to cache...
                            objCache = q.ToList();
                            //10. Cache the result but use the already created objectCache. Or else the Linq-query will be executed once more...
                            System.Web.HttpRuntime.Cache.Insert(CacheId, objCache, sqldep);
                        }
                    }
                }
                //Return the created (or cached) List
                return objCache;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
&lt;/pre&gt; &lt;br /&gt;&lt;h3&gt;
IMPORTANT
&lt;/h3&gt;You need to call SqlDependency.Start for example in global.asax&lt;br /&gt;&lt;pre&gt;
   //In Application Start Event
   System.Data.SqlClient.SqlDependency.Start(&amp;quot;DataContextConnectionString&amp;quot;);
   //In Application End Event
   System.Data.SqlClient.SqlDependency.Stop(&amp;quot;DataContextConnectionString&amp;quot;);
&lt;/pre&gt; &lt;br /&gt;&lt;h2&gt;
Calling the Extension Method
&lt;/h2&gt;&lt;span class="unresolved"&gt;Cannot resolve link: &lt;/span&gt;[file:_203430687.png_]&lt;br /&gt;&lt;b&gt;Resource Page Description&lt;/b&gt;&lt;br /&gt;Building a extensionmethod for caching LINQ results with SqlCacheDependency
&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&lt;b&gt;**Delete the following note before publishing **&lt;/b&gt;&lt;br /&gt; &lt;br /&gt;This resource page is currently in setup mode and only available to coordinators and developers. Once you have finished setting up your resource page you can publish it to make it available to all MSDN Code Gallery visitors.&lt;br /&gt; &lt;br /&gt;To get your Resource Page ready to publish, you should do the following:&lt;br /&gt;&lt;ol&gt;
&lt;li&gt;Make any changes to the details of your resource page&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;Here you can enable or disable functions of your resource page.  You might want to turn on the Issue Tracker to allow users to provide feedback on your resource, or if you have a resource that does not involve a code sample, you may want to turn off the Releases tab.&lt;/li&gt;&lt;li&gt;Make sure your resource page description is detailed enough to let people search for your resource.&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;Add your code sample or other resources to the resource page&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;If you’re uploading code, go to the Releases tab and create a new release to house your code.  Creating a release allows you to have the license properly displayed when people download your code, as well as provides a download count.&lt;/li&gt;&lt;li&gt;Edit your Wiki page to attach any resources you may have that are not source code.&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;If you want to let someone see your resource page before it is published, go to the People tab and add them to your resource page&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;This will let you add other team members who may be contributing to your resource, or just show it off and get feedback from someone you trust.&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;Tag your resource page with descriptive tags to make it easier for people to find your resources when browsing the gallery.&lt;/li&gt;&lt;li&gt;Publish your resource page so it becomes visible to everyone!&lt;/li&gt;
&lt;/ol&gt; &lt;br /&gt;Additional information on starting a new resource page is available here: &lt;a href="http://code.msdn.microsoft.com/CodeGallery" class="externalLink"&gt;Resource Page Startup Guide&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;.&lt;br /&gt;
&lt;/div&gt;</description><author>UTB</author><pubDate>Tue, 08 Jul 2008 18:50:37 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: Home 20080708P</guid></item><item><title>UPDATED WIKI: Home</title><link>http://code.msdn.microsoft.com/linqtosqlcache/Wiki/View.aspx?title=Home&amp;version=6</link><description>&lt;div class="wikidoc"&gt;
&lt;h2&gt;
What&amp;#180;s this?
&lt;/h2&gt;This page and any other page I may publish on this site contains some code that I&amp;#180;ve written in my projects.&lt;br /&gt;I&amp;#180;ll use these pages as a repository for reusable code, bur it&amp;#180;s always fun if someone else can use (or comment, good or bad) the code.&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
Caching Linq results using SqlCacheDependency
&lt;/h2&gt;In early 2008 I needed to cache the results from LinqToSql-queries. I like the SqlCacheDependency (especially in SQL 2K5 Server).&lt;br /&gt;I also like the new extension methods posibillity... So this is what I came up with.&lt;br /&gt; &lt;br /&gt;The code should be explained enough by the comments I think.&lt;br /&gt;&lt;pre&gt;
    public static class Cache
    {
        /// &amp;lt;summary&amp;gt;
        /// Caches any Linq query that is created for LinqToSql.
        /// Limits are the same as SqlCacheDependency
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;typeparam name=&amp;quot;T&amp;quot;&amp;gt;&amp;lt;/typeparam&amp;gt;
        /// &amp;lt;param name=&amp;quot;q&amp;quot;&amp;gt;The linq query&amp;lt;/param&amp;gt;
        /// &amp;lt;param name=&amp;quot;dc&amp;quot;&amp;gt;Your LinqToSql DataContext&amp;lt;/param&amp;gt;
        /// &amp;lt;param name=&amp;quot;CacheId&amp;quot;&amp;gt;The unique Id for the cache&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
        public static List&amp;lt;T&amp;gt; LinqCache&amp;lt;T&amp;gt;(this System.Linq.IQueryable&amp;lt;T&amp;gt; q, System.Data.Linq.DataContext dc, string CacheId)
        {
            try
            {
                List&amp;lt;T&amp;gt; objCache = (List&amp;lt;T&amp;gt;)System.Web.HttpRuntime.Cache.Get(CacheId);
                if (objCache == null)
                {
                    /////////No cache... implement new SqlCacheDependeny//////////
                    //1. Get connstring from DataContext
                    string connStr = dc.Connection.ConnectionString;
                    //2. Get SqlCommand from DataContext and the LinqQuery
                    string sqlCmd = dc.GetCommand(q).CommandText;
                    //3. Create Conn to use in SqlCacheDependency
                    using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr))
                    {
                        conn.Open();
                        //4. Create Command to use in SqlCacheDependency
                        using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sqlCmd, conn))
                        {
                            //5.1 Enable DB for Notifications... Only needed once per DB...
                            System.Web.Caching.SqlCacheDependencyAdmin.EnableNotifications(connStr);
                            //5.2 Get ElementType for the query
                            string NotificationTable = q.ElementType.Name;
                            //5.2 Enable the elementtype for notification (if not done!)
                            if (!System.Web.Caching.SqlCacheDependencyAdmin.GetTablesEnabledForNotifications(connStr).Contains(NotificationTable))
                                System.Web.Caching.SqlCacheDependencyAdmin.EnableTableForNotifications(connStr, NotificationTable);
                            //6. Create SqlCacheDependency
                            System.Web.Caching.SqlCacheDependency sqldep = new System.Web.Caching.SqlCacheDependency(cmd);
                            //7. Refresh the LinqQuery from DB so that we will not use the current Linq cache
                            dc.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, q);
                            //8. Execute SqlCacheDepency query...
                            cmd.ExecuteNonQuery();
                            //9. Execute LINQ-query to have something to cache...
                            objCache = q.ToList();
                            //10. Cache the result but use the already created objectCache. Or else the Linq-query will be executed once more...
                            System.Web.HttpRuntime.Cache.Insert(CacheId, objCache, sqldep);
                        }
                    }
                }
                //Return the created (or cached) List
                return objCache;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
&lt;/pre&gt; &lt;br /&gt;&lt;h3&gt;
IMPORTANT
&lt;/h3&gt;You need to call SqlDependency.Start for example in global.asax&lt;br /&gt;&lt;pre&gt;
   //In Application Start Event
   System.Data.SqlClient.SqlDependency.Start(&amp;quot;DataContextConnectionString&amp;quot;);
   //In Application End Event
   System.Data.SqlClient.SqlDependency.Stop(&amp;quot;DataContextConnectionString&amp;quot;);
&lt;/pre&gt;&lt;b&gt;Resource Page Description&lt;/b&gt;&lt;br /&gt;Building a extensionmethod for caching LINQ results with SqlCacheDependency
&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&lt;b&gt;**Delete the following note before publishing **&lt;/b&gt;&lt;br /&gt; &lt;br /&gt;This resource page is currently in setup mode and only available to coordinators and developers. Once you have finished setting up your resource page you can publish it to make it available to all MSDN Code Gallery visitors.&lt;br /&gt; &lt;br /&gt;To get your Resource Page ready to publish, you should do the following:&lt;br /&gt;&lt;ol&gt;
&lt;li&gt;Make any changes to the details of your resource page&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;Here you can enable or disable functions of your resource page.  You might want to turn on the Issue Tracker to allow users to provide feedback on your resource, or if you have a resource that does not involve a code sample, you may want to turn off the Releases tab.&lt;/li&gt;&lt;li&gt;Make sure your resource page description is detailed enough to let people search for your resource.&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;Add your code sample or other resources to the resource page&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;If you’re uploading code, go to the Releases tab and create a new release to house your code.  Creating a release allows you to have the license properly displayed when people download your code, as well as provides a download count.&lt;/li&gt;&lt;li&gt;Edit your Wiki page to attach any resources you may have that are not source code.&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;If you want to let someone see your resource page before it is published, go to the People tab and add them to your resource page&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;This will let you add other team members who may be contributing to your resource, or just show it off and get feedback from someone you trust.&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;Tag your resource page with descriptive tags to make it easier for people to find your resources when browsing the gallery.&lt;/li&gt;&lt;li&gt;Publish your resource page so it becomes visible to everyone!&lt;/li&gt;
&lt;/ol&gt; &lt;br /&gt;Additional information on starting a new resource page is available here: &lt;a href="http://code.msdn.microsoft.com/CodeGallery" class="externalLink"&gt;Resource Page Startup Guide&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;.&lt;br /&gt;
&lt;/div&gt;</description><author>UTB</author><pubDate>Tue, 08 Jul 2008 18:48:07 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: Home 20080708P</guid></item><item><title>UPDATED WIKI: Home</title><link>http://code.msdn.microsoft.com/linqtosqlcache/Wiki/View.aspx?title=Home&amp;version=5</link><description>&lt;div class="wikidoc"&gt;
&lt;h2&gt;
What&amp;#180;s this?
&lt;/h2&gt;This page and any other page I may publish on this site contains some code that I&amp;#180;ve written in my projects.&lt;br /&gt;I&amp;#180;ll use these pages as a repository for reusable code, bur it&amp;#180;s always fun if someone else can use (or comment, good or bad) the code.&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
Caching Linq results using SqlCacheDependency
&lt;/h2&gt;In early 2008 I needed to cache the results from LinqToSql-queries. I like the SqlCacheDependency (especially in SQL 2K5 Server).&lt;br /&gt;I also like the new extension methods posibillity... So this is what I came up with.&lt;br /&gt; &lt;br /&gt;The code should be explained enough by the comments I think.&lt;br /&gt;&lt;pre&gt;
    public static class Cache
    {
        /// &amp;lt;summary&amp;gt;
        /// Caches any Linq query that is created for LinqToSql.
        /// Limits are the same as SqlCacheDependency
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;typeparam name=&amp;quot;T&amp;quot;&amp;gt;&amp;lt;/typeparam&amp;gt;
        /// &amp;lt;param name=&amp;quot;q&amp;quot;&amp;gt;The linq query&amp;lt;/param&amp;gt;
        /// &amp;lt;param name=&amp;quot;dc&amp;quot;&amp;gt;Your LinqToSql DataContext&amp;lt;/param&amp;gt;
        /// &amp;lt;param name=&amp;quot;CacheId&amp;quot;&amp;gt;The unique Id for the cache&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
        public static List&amp;lt;T&amp;gt; LinqCache&amp;lt;T&amp;gt;(this System.Linq.IQueryable&amp;lt;T&amp;gt; q, System.Data.Linq.DataContext dc, string CacheId)
        {
            try
            {
                List&amp;lt;T&amp;gt; objCache = (List&amp;lt;T&amp;gt;)System.Web.HttpRuntime.Cache.Get(CacheId);
                if (objCache == null)
                {
                    /////////No cache... implement new SqlCacheDependeny//////////
                    //1. Get connstring from DataContext
                    string connStr = dc.Connection.ConnectionString;
                    //2. Get SqlCommand from DataContext and the LinqQuery
                    string sqlCmd = dc.GetCommand(q).CommandText;
                    //3. Create Conn to use in SqlCacheDependency
                    using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr))
                    {
                        conn.Open();
                        //4. Create Command to use in SqlCacheDependency
                        using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sqlCmd, conn))
                        {
                            //5.1 Enable DB for Notifications... Only needed once per DB...
                            System.Web.Caching.SqlCacheDependencyAdmin.EnableNotifications(connStr);
                            //5.2 Get ElementType for the query
                            string NotificationTable = q.ElementType.Name;
                            //5.2 Enable the elementtype for notification (if not done!)
                            if (!System.Web.Caching.SqlCacheDependencyAdmin.GetTablesEnabledForNotifications(connStr).Contains(NotificationTable))
                                System.Web.Caching.SqlCacheDependencyAdmin.EnableTableForNotifications(connStr, NotificationTable);
                            //6. Create SqlCacheDependency
                            System.Web.Caching.SqlCacheDependency sqldep = new System.Web.Caching.SqlCacheDependency(cmd);
                            //7. Refresh the LinqQuery from DB so that we will not use the current Linq cache
                            dc.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, q);
                            //8. Execute SqlCacheDepency query...
                            cmd.ExecuteNonQuery();
                            //9. Execute LINQ-query to have something to cache...
                            objCache = q.ToList();
                            //10. Cache the result but use the already created objectCache. Or else the Linq-query will be executed once more...
                            System.Web.HttpRuntime.Cache.Insert(CacheId, objCache, sqldep);
                        }
                    }
                }
                //Return the created (or cached) List
                return objCache;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
&lt;/pre&gt;&lt;b&gt;Resource Page Description&lt;/b&gt;&lt;br /&gt;Building a extensionmethod for caching LINQ results with SqlCacheDependency
&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&lt;b&gt;**Delete the following note before publishing **&lt;/b&gt;&lt;br /&gt; &lt;br /&gt;This resource page is currently in setup mode and only available to coordinators and developers. Once you have finished setting up your resource page you can publish it to make it available to all MSDN Code Gallery visitors.&lt;br /&gt; &lt;br /&gt;To get your Resource Page ready to publish, you should do the following:&lt;br /&gt;&lt;ol&gt;
&lt;li&gt;Make any changes to the details of your resource page&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;Here you can enable or disable functions of your resource page.  You might want to turn on the Issue Tracker to allow users to provide feedback on your resource, or if you have a resource that does not involve a code sample, you may want to turn off the Releases tab.&lt;/li&gt;&lt;li&gt;Make sure your resource page description is detailed enough to let people search for your resource.&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;Add your code sample or other resources to the resource page&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;If you’re uploading code, go to the Releases tab and create a new release to house your code.  Creating a release allows you to have the license properly displayed when people download your code, as well as provides a download count.&lt;/li&gt;&lt;li&gt;Edit your Wiki page to attach any resources you may have that are not source code.&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;If you want to let someone see your resource page before it is published, go to the People tab and add them to your resource page&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;This will let you add other team members who may be contributing to your resource, or just show it off and get feedback from someone you trust.&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;Tag your resource page with descriptive tags to make it easier for people to find your resources when browsing the gallery.&lt;/li&gt;&lt;li&gt;Publish your resource page so it becomes visible to everyone!&lt;/li&gt;
&lt;/ol&gt; &lt;br /&gt;Additional information on starting a new resource page is available here: &lt;a href="http://code.msdn.microsoft.com/CodeGallery" class="externalLink"&gt;Resource Page Startup Guide&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;.&lt;br /&gt;
&lt;/div&gt;</description><author>UTB</author><pubDate>Tue, 08 Jul 2008 18:42:25 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: Home 20080708P</guid></item><item><title>UPDATED WIKI: Home</title><link>http://code.msdn.microsoft.com/linqtosqlcache/Wiki/View.aspx?title=Home&amp;version=4</link><description>&lt;div class="wikidoc"&gt;
//multiline&lt;br /&gt;&lt;pre&gt;
public static void JustATest()
{
   try
   {
      //code here
   }
   catch(Exception ex)
   {
      throw ex,
   }
}
&lt;/pre&gt;&lt;b&gt;Resource Page Description&lt;/b&gt;&lt;br /&gt;Building a extensionmethod for caching LINQ results with SqlCacheDependency
&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&lt;b&gt;**Delete the following note before publishing **&lt;/b&gt;&lt;br /&gt; &lt;br /&gt;This resource page is currently in setup mode and only available to coordinators and developers. Once you have finished setting up your resource page you can publish it to make it available to all MSDN Code Gallery visitors.&lt;br /&gt; &lt;br /&gt;To get your Resource Page ready to publish, you should do the following:&lt;br /&gt;&lt;ol&gt;
&lt;li&gt;Make any changes to the details of your resource page&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;Here you can enable or disable functions of your resource page.  You might want to turn on the Issue Tracker to allow users to provide feedback on your resource, or if you have a resource that does not involve a code sample, you may want to turn off the Releases tab.&lt;/li&gt;&lt;li&gt;Make sure your resource page description is detailed enough to let people search for your resource.&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;Add your code sample or other resources to the resource page&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;If you’re uploading code, go to the Releases tab and create a new release to house your code.  Creating a release allows you to have the license properly displayed when people download your code, as well as provides a download count.&lt;/li&gt;&lt;li&gt;Edit your Wiki page to attach any resources you may have that are not source code.&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;If you want to let someone see your resource page before it is published, go to the People tab and add them to your resource page&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;This will let you add other team members who may be contributing to your resource, or just show it off and get feedback from someone you trust.&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;Tag your resource page with descriptive tags to make it easier for people to find your resources when browsing the gallery.&lt;/li&gt;&lt;li&gt;Publish your resource page so it becomes visible to everyone!&lt;/li&gt;
&lt;/ol&gt; &lt;br /&gt;Additional information on starting a new resource page is available here: &lt;a href="http://code.msdn.microsoft.com/CodeGallery" class="externalLink"&gt;Resource Page Startup Guide&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;.&lt;br /&gt;
&lt;/div&gt;</description><author>UTB</author><pubDate>Tue, 08 Jul 2008 13:50:11 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: Home 20080708P</guid></item><item><title>UPDATED WIKI: Home</title><link>http://code.msdn.microsoft.com/linqtosqlcache/Wiki/View.aspx?title=Home&amp;version=3</link><description>&lt;div class="wikidoc"&gt;
//multiline&lt;br /&gt;&lt;pre&gt;
public static void JustATest()
{
try
{
code here
}
catch(Exception ex)
{
throw ex,
}
}
&lt;/pre&gt;&lt;b&gt;Resource Page Description&lt;/b&gt;&lt;br /&gt;Building a extensionmethod for caching LINQ results with SqlCacheDependency
&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&lt;b&gt;**Delete the following note before publishing **&lt;/b&gt;&lt;br /&gt; &lt;br /&gt;This resource page is currently in setup mode and only available to coordinators and developers. Once you have finished setting up your resource page you can publish it to make it available to all MSDN Code Gallery visitors.&lt;br /&gt; &lt;br /&gt;To get your Resource Page ready to publish, you should do the following:&lt;br /&gt;&lt;ol&gt;
&lt;li&gt;Make any changes to the details of your resource page&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;Here you can enable or disable functions of your resource page.  You might want to turn on the Issue Tracker to allow users to provide feedback on your resource, or if you have a resource that does not involve a code sample, you may want to turn off the Releases tab.&lt;/li&gt;&lt;li&gt;Make sure your resource page description is detailed enough to let people search for your resource.&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;Add your code sample or other resources to the resource page&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;If you’re uploading code, go to the Releases tab and create a new release to house your code.  Creating a release allows you to have the license properly displayed when people download your code, as well as provides a download count.&lt;/li&gt;&lt;li&gt;Edit your Wiki page to attach any resources you may have that are not source code.&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;If you want to let someone see your resource page before it is published, go to the People tab and add them to your resource page&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;This will let you add other team members who may be contributing to your resource, or just show it off and get feedback from someone you trust.&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;Tag your resource page with descriptive tags to make it easier for people to find your resources when browsing the gallery.&lt;/li&gt;&lt;li&gt;Publish your resource page so it becomes visible to everyone!&lt;/li&gt;
&lt;/ol&gt; &lt;br /&gt;Additional information on starting a new resource page is available here: &lt;a href="http://code.msdn.microsoft.com/CodeGallery" class="externalLink"&gt;Resource Page Startup Guide&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;.&lt;br /&gt;
&lt;/div&gt;</description><author>UTB</author><pubDate>Tue, 08 Jul 2008 13:48:48 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: Home 20080708P</guid></item><item><title>UPDATED WIKI: Home</title><link>http://code.msdn.microsoft.com/linqsqlcache/Wiki/View.aspx?title=Home&amp;version=2</link><description>&lt;div class="wikidoc"&gt;
//multiline&lt;br /&gt;&lt;pre&gt;
void context_Error(object sender, EventArgs e)
    {
        try
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
 
            if (!System.IO.File.Exists(this.oApplication.Context.Request.PhysicalApplicationPath + &amp;quot;\\&amp;quot; + this.fileErr))
            {
                this.swErr = new System.IO.StreamWriter(this.oApplication.Context.Request.PhysicalApplicationPath + &amp;quot;\\&amp;quot; + this.fileErr, false);
                this.swErr.WriteLine(&amp;quot;TIME;REQUESTTYPE;ERRORMESSAGE;EXCEPTION;BASEEXCEPTION;URL;QUERYSTRING;CONTENTTYPE;USERAGENT;USERHOSTADRESS;USERHOSTNAME&amp;quot;);
            }
            else
                this.swErr = new System.IO.StreamWriter(this.oApplication.Context.Request.PhysicalApplicationPath + &amp;quot;\\&amp;quot; + this.fileErr, true);
            sb.Append(DateTime.Now.ToString() + &amp;quot;;&amp;quot;);
            sb.Append(this.oApplication.Context.Request.RequestType + &amp;quot;;&amp;quot;);
            sb.Append(this.oApplication.Context.Error.Message.Replace(&amp;quot;;&amp;quot;, &amp;quot;|&amp;quot;) + &amp;quot;;&amp;quot;);
            sb.Append(this.oApplication.Context.Error.GetType().FullName + &amp;quot;;&amp;quot;);
            sb.Append(this.oApplication.Context.Error.GetBaseException().GetType().FullName + &amp;quot;;&amp;quot;);
            sb.Append(this.oApplication.Context.Request.Url + &amp;quot;;&amp;quot;);
            sb.Append(this.oApplication.Context.Request.QueryString + &amp;quot;;&amp;quot;);
            sb.Append(this.oApplication.Context.Request.ContentType + &amp;quot;;&amp;quot;);
            sb.Append(this.oApplication.Context.Request.UserAgent.Replace(&amp;quot;;&amp;quot;, &amp;quot;|&amp;quot;) + &amp;quot;;&amp;quot;);
            sb.Append(this.oApplication.Context.Request.UserHostAddress + &amp;quot;;&amp;quot;);
            sb.Append(this.oApplication.Context.Request.UserHostName + &amp;quot;;&amp;quot;);
 
            this.swErr.WriteLine(sb.ToString());
            this.swErr.Flush();
            this.swErr.Close();
            this.swErr.Dispose();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
&lt;/pre&gt;&lt;b&gt;Resource Page Description&lt;/b&gt;&lt;br /&gt;Building a extensionmethod for caching LINQ results with SqlCacheDependency
&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&lt;b&gt;**Delete the following note before publishing **&lt;/b&gt;&lt;br /&gt; &lt;br /&gt;This resource page is currently in setup mode and only available to coordinators and developers. Once you have finished setting up your resource page you can publish it to make it available to all MSDN Code Gallery visitors.&lt;br /&gt; &lt;br /&gt;To get your Resource Page ready to publish, you should do the following:&lt;br /&gt;&lt;ol&gt;
&lt;li&gt;Make any changes to the details of your resource page&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;Here you can enable or disable functions of your resource page.  You might want to turn on the Issue Tracker to allow users to provide feedback on your resource, or if you have a resource that does not involve a code sample, you may want to turn off the Releases tab.&lt;/li&gt;&lt;li&gt;Make sure your resource page description is detailed enough to let people search for your resource.&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;Add your code sample or other resources to the resource page&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;If you’re uploading code, go to the Releases tab and create a new release to house your code.  Creating a release allows you to have the license properly displayed when people download your code, as well as provides a download count.&lt;/li&gt;&lt;li&gt;Edit your Wiki page to attach any resources you may have that are not source code.&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;If you want to let someone see your resource page before it is published, go to the People tab and add them to your resource page&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;This will let you add other team members who may be contributing to your resource, or just show it off and get feedback from someone you trust.&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;Tag your resource page with descriptive tags to make it easier for people to find your resources when browsing the gallery.&lt;/li&gt;&lt;li&gt;Publish your resource page so it becomes visible to everyone!&lt;/li&gt;
&lt;/ol&gt; &lt;br /&gt;Additional information on starting a new resource page is available here: &lt;a href="http://code.msdn.microsoft.com/CodeGallery" class="externalLink"&gt;Resource Page Startup Guide&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;.&lt;br /&gt;
&lt;/div&gt;</description><author>UTB</author><pubDate>Tue, 08 Jul 2008 13:16:56 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: Home 20080708P</guid></item></channel></rss>