title: LINQ /Entity Framework complex query: take all rows
body: <p>First, excuse me for the title as I couldnt find a better meaningful title for the problem  that I am trying to solve.</p>
  
  <p>I have 3 tables,  Companies, Devices, and MEasures, A company can have many devices, and the measures table has the temperature data of each device every 5 minutes.</p>
  
  <p>I drawed a table with this:
  <a href="https://i.stack.imgur.com/q4Bgq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/q4Bgq.png" alt="enter image description here"></a></p>
  
  <p>The problem is:</p>
  
  <p>I need to make a LINQ/EF query that returns me:
  1. The last temperature of every single device, per company.</p>
  
  <p>So if you see the tablees in the picture, it should return this:</p>
  
  <p><a href="https://i.stack.imgur.com/33smt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/33smt.png" alt="enter image description here"></a></p>
  
  <p>My model in C# is like this:</p>
  
  <pre><code>public class Medida
      {
          [Key]
          [DisplayName("Medida")]
          public int MedidaID { get; set; }
  
          public decimal Temperatura { get; set; }
          public int Humedad { get; set; }
  
          public DateTime FechaHora { get; set; }
  
          [DisplayName("Dispositivo")]        
          public int DispositivoID { get; set; }
          public virtual Dispositivo Dispositivo { get; set; }
  
  
  
      }
  </code></pre>
  
  <p>Devices:</p>
  
  <pre><code>    [Table("Dispositivo")]
      public class Dispositivo  // 
      {
          [Key]
          [Column(Order = 0)]
  
          public int DispositivoID { get; set; }
  
          [StringLength(100)]
          [Required(ErrorMessage = "El nombre  es requerido")]
          public string Nombre { get; set; }
  
          public string ClaveDispositivo { get; set; }
  
          public bool Activo { get; set; }
          [Required(ErrorMessage = "El nombre Empresa es requerido")]
          [Display(Name = "Empresa")]
          public int EmpresaID { get; set; }
  
          public virtual Empresa Empresa { get; set; }
  
          public virtual ICollection&lt;Medida&gt; Medidas { get; set; }
      }
  }
  </code></pre>
  
  <p>And the company table</p>
  
  <pre><code>  public partial class Empresa
      {
          [Key] //clave primaria en la tabla
          [Display(Name = "Empresa")]
          public int EmpresaID { get; set; }//metodos de acceso de lectura y escritura a la propiedad
  
          [StringLength(100)] // no debe pasarse de 100 caracteres
          [Required(ErrorMessage = "El nombre empresa es requerido")]
          [DisplayName("Nombre Empresa")]
          public string Nombre { get; set; }
  
          [StringLength(100)]
          //[Required(ErrorMessage = "El código es requerido")]
          [DisplayName("Código")]
          public string Codigo { get; set; } 
  
          [Display(Name = "Correo")]
          [Required(ErrorMessage = "El correo es requerido")]
          [EmailAddress(ErrorMessage = "dirección de correo invalido")]
  
          public string Correo { get; set; }
          public bool Activo { get; set; }
          public bool Notificar_Correo { get; set; }
          public bool Notificar_SMS { get; set; }
  
          public virtual ICollection&lt;Dispositivo&gt; Dispositivos { get; set; }
          public virtual ICollection&lt;Usuario&gt; Usuarios { get; set; }
      }
  </code></pre>
  
  <p>And finally this is the code I am trying to implement where I need to restrict the rows returned:</p>
  
  <pre><code> public void TakeLastTemp()
              {
  
                  using (ZigBeeContext db = new ZigBeeContext())
                  {
                      var lista = db.Medidas
                                   .Include(e =&gt; e.Dispositivo);
  </code></pre>