title: ADO.NET Navigation Properties
body: <p>I'm having a little problem using Navigation properties and inheritance on ADO.NET.</p>
  
  <p>This is my Data Model:</p>
  
  <p><img src="https://i.stack.imgur.com/gGtPO.png" alt="ADO.NET Data Model"></p>
  
  <p>First, some vocabulary:</p>
  
  <blockquote>
    <p>Categoria = Category<br>
    Formulario = Form<br>
    Campo = Field<br>
    Imagem = Image<br>
    Paragrafo = Paragraph<br>
    Escolha = Choice<br>
    Texto = Text<br>
    Resposta = Answer</p>
  </blockquote>
  
  <p>So, I'm trying to create a custom property on a Form returning it's answers count.</p>
  
  <p>The normal approach (i think) would be:</p>
  
  <pre><code>public partial class Formulario
  {
      public int Respostas
      {
          get
          {
              List&lt;Item&gt; itens = this.Itens.ToList();
  
              IEnumerable&lt;Campo&gt; campos = from m in itens where m as Campo != null select (Campo)m;
              int soma = campos.Sum(m =&gt; m.Respostas.Count);
  
              return soma;
          }
      }
  }
  </code></pre>
  
  <p>But it doesn't work. The <code>itens</code> list returns 0 elements. But when I do as below, it return the 4 items it should:</p>
  
  <pre><code>public partial class Formulario
  {
      public int Respostas
      {
          get
          {
              FormulariosDB db = new FormulariosDB();
  
              List&lt;Item&gt; itens = db.Items.Where(m =&gt; m.Formulario.Id == this.Id).ToList();
  
              IEnumerable&lt;Campo&gt; campos = from m in itens where m as Campo != null select (Campo)m;
              int soma = campos.Sum(m =&gt; m.Respostas.Count);
  
              return soma;
          }
      }
  }
  </code></pre>
  
  <p>It only works when I instanciate the entire data model. Does anyone knows why?</p>
  
  <p>PS: I'm using the <code>.toList()</code> method so I can use all <code>Linq</code> queries, not only the ones <code>Linq2Entities</code> allows me to</p>