title: MVC How to count records in related Models?
body: <p>I have two models, First is Relations, which is opened with Date, second model is Reservations, now i need to count record in reservations which have choiced Date of Relations. The tables is in Relationship relID from first i record in second table in DatumRID.</p>
  
  <p>How to count records in Reservations which is related by ID to Relations</p>
  
  <p>Model Relations:</p>
  
  <pre><code>  public tbl_relacii()
          {
              tbl_rezervacii = new HashSet&lt;tbl_rezervacii&gt;();
          }
          [Key]
          public int relID { get; set; }
          [Column(TypeName = "date")]
          [DataType(DataType.Date)]
          [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
          public DateTime DatumR { get; set; }
          public int sedista { get; set; }
          public string vozilo { get; set; }
          [StringLength(50)]
          public string shofer1 { get; set; }
          [StringLength(50)]
          public string shofer2 { get; set; }
          public string noteR { get; set; }
          public virtual ICollection&lt;tbl_rezervacii&gt; tbl_rezervacii { get; set; }
          public string DatumForDisplay
          {
              get
              {
                  return DatumR.ToString("d");
              }
          }
  </code></pre>
  
  <p>Model Reservations:</p>
  
  <pre><code> public partial class tbl_rezervacii
      {
          [Key]
          public int rID { get; set; }
          public int AgentID { get; set; }
          [StringLength(10)]
          public string karta_br { get; set; }
          public int DatumRID { get; set; }
          public int patnikID { get; set; }
          public int stanicaOD { get; set; }
          public int stanicaDO { get; set; }
          public decimal cena { get; set; }
          public bool povratna { get; set; }
          public DateTime? DatumP { get; set; }
          public string noteP { get; set; }
          public virtual tbl_agenti tbl_agenti { get; set; }
          public virtual tbl_patnici tbl_patnici { get; set; }
          public virtual tbl_relacii tbl_relacii { get; set; }
          public virtual tbl_stanici tbl_stanici { get; set; }
          public virtual tbl_stanici tbl_stanici1 { get; set; }
         public string relacija
          {
              get
              {
                  return tbl_stanici.stanica + "=&gt;" + tbl_stanici1.stanica;
              }
          }
          public string relacijaP
          {
              get
              {
                  return tbl_stanici.stanica + "=&gt;" + tbl_stanici1.stanica + "=&gt;" + tbl_stanici.stanica;
              }
          }
      }
  </code></pre>
  
  <p>And here is Controller for Relations Index:</p>
  
  <pre><code>  public ActionResult Index()
          {
                 return View(db.tbl_relacii.ToList().OrderByDescending(x =&gt; x.DatumR));
          }
  </code></pre>
  
  <p>How to count records in Reservations then put Number of records to index of Relations?</p>