title: C# LINQ to SQL Specified type member inside lambda expression
body: <p>I understand that LINQ can't use properties that are not mapped to a database column, though I don't understand why one LINQ statement works inside a non static method but I get this error when attempting within one.</p>
  <p>Here's my <strong>working</strong> method:</p>
  <pre><code>private TemplatesAPIContext db = new TemplatesAPIContext();
  
  // GET api/Template
  public IQueryable&lt;TemplateDto&gt; GetTemplates()
  {
      return db.TemplateModels.Include(t =&gt; t.Categories).Select(
          x =&gt; new TemplateDto
          {
              TemplateID = x.TemplateID,
              Name = x.Name,
              HTMLShowcase = x.HTMLShowcase,
              ShortDescription = x.ShortDescription,
              CreationDate = x.CreationDate,
              Downloads = x.Downloads,
              Tags = x.Tags,
              Categories = db.CategoryModels
                              .Where(c =&gt; x.Categories.Where(a =&gt; a.TemplateID == x.TemplateID)
                              .Select(a =&gt; a.CategoryID).Contains(c.CategoryID))
          }
      );
  }
  </code></pre>
  <p>I don't want to repeat myself with this complex building of a DTO (I actually still need to add some other relationships still to it and it will get much more complex) and type this out on every method in the controller so I wanted to make a lambda expression and pass it to the methods.</p>
  <p>So I did this:</p>
  <pre><code>private static readonly Expression&lt;Func&lt;TemplateModel, TemplateDto&gt;&gt; AsTemplateDto =
      x =&gt; new TemplateDto
      {
          TemplateID = x.TemplateID,
          Name = x.Name,
          HTMLShowcase = x.HTMLShowcase,
          ShortDescription = x.ShortDescription,
          CreationDate = x.CreationDate,
          Downloads = x.Downloads,
          Tags = x.Tags,
          Categories = new TemplatesAPIContext().CategoryModels
                              .Where(c =&gt; x.Categories.Where(a =&gt; a.TemplateID == x.TemplateID)
                              .Select(a =&gt; a.CategoryID).Contains(c.CategoryID))
      };
  </code></pre>
  <p>In the hopes of calling:</p>
  <pre><code>// GET api/Template
  public IQueryable&lt;TemplateDto&gt; GetTemplates()
  {
      return db.TemplateModels.Include(t =&gt; t.Categories).Select(AsTemplateDto);
  }
  </code></pre>
  <p>But this returns this error, which doesn't make sense to me since its the exact same query, only difference being that I need to instantiate the dbContext in the lambda since I can't use the one instantiated in the controller as the lambda expression is static.</p>
  <h3>Error</h3>
  <blockquote>
  <p>The specified type member 'CategoryModels' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.</p>
  </blockquote>