title: Linq to Sql caching
body: <p>I have a Decorator around a Linq DataContext which I want to use for caching. Repository returns an IQueryable of one of the mapped Database types. Is this a good idea if my intent was to improve performance ? Sometimes, the method will return be an IQueryable coming from a List, and other times, it will come directly from the DataContext. With this configuration, some queries crash with a Stackoverflowexception. </p>
  
  <p>The query :</p>
  
  <pre><code>         IDataContext ctx = DataContextFactory.ctx();
       IQueryable&lt;PositionsVF&gt; positions = from p in ctx.Repository&lt;PositionsVF&gt;()
                                           from c in ctx.Repository&lt;ConfigurationPortefeuillesModule&gt;()
  
                                           where c.Module.Module1 == ListeModules.POSITIONS_VF.ToString() &amp;&amp;
                                           p.NoPortefeuille_FK == c.NoPortefeuille_FK &amp;&amp; 
                                           p.PortefeuillesSpecialise.Categorie != "REGROUPEMENT" &amp;&amp;
                                           p.NoFond_FK == fonds &amp;&amp;
                                           p.Date == dateDonneeBD
  
                                           orderby c.Ordre
                                           select p;
  </code></pre>
  
  <p>The code:</p>
  
  <pre><code>  Dictionary&lt;string, List&lt;object&gt;&gt; CachedTables = new Dictionary&lt;string, List&lt;object&gt;&gt;();
  
    // Small tables which are queried against a lot.
    List&lt;string&gt; ShouldCache = new List&lt;string&gt;() { 
       //typeof(DerniersSoldesDAV).Name,
       //typeof(Fond).Name,
       //typeof(PortefeuillesSpecialise).Name,
       //typeof(PortefeuillesSuperposition).Name,
       //typeof(PositionsVF).Name,
       //typeof(PoidsACWI).Name,
       //typeof(PositionsSASMurex).Name,
       //typeof(TypesCompte).Name,
       //typeof(TypesMontant).Name,
       //typeof(Module).Name,
  
       typeof(CorrespondanceIndice).Name,
       typeof(CorrespondancePortefeuille).Name,
       //typeof(CorrespondancePortefeuillesPGP).Name,
       //typeof(CorrespondanceRegroupement).Name,
       //typeof(CorrespondancesPortefeuillesSuperposition).Name,
  
       //typeof(ConfigurationFondsModule).Name,
       //typeof(ConfigurationIndicesModule).Name,
       //typeof(ConfigurationPortefeuillesFondsModule).Name,
       //typeof(ConfigurationPortefeuillesModule).Name,
  
    };
  
    public IQueryable&lt;T&gt; Repository&lt;T&gt;() where T : class
    {
       ITable table = _context.GetTable(typeof(T));
       string typeName = typeof(T).Name;
  
       if (ShouldCache.Contains(typeName))
       {
          // Load the mini table.
          if (! CachedTables.ContainsKey(typeName))
          {
             var tableRows = table.Cast&lt;T&gt;().ToList();
             CachedTables.Add(typeName, tableRows.Cast&lt;object&gt;().ToList());
          }
  
          return CachedTables[typeName].Cast&lt;T&gt;().AsQueryable();
       }
  
  
       return table.Cast&lt;T&gt;();
    }
  </code></pre>