title: Using navigation properties instead of LINQ Join in WebApi controller
body: <p>I am using Entity Framework Code First and LINQ to Entities in a Web Api Controller. </p>
  
  <p>I have 3 tables that are related with the following relations/ navigation properties: (the model classes are as follows)</p>
  
  <p>My Reservation class:</p>
  
  <pre><code>[Table("Reservations")] 
  public class Reservation
  {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int ReservationId { get; set; }
      public DateTime Date { get; set; }
  
      public int RoomId { get; set; }
      public Room Room { get; set; }
  
  
  
      public Customer Customer { get; set; }
      public int CustomerId { get; set; }
  
      // create association table ReservationMeals
      public virtual ICollection&lt;ReservationMealItems&gt; ReservationMeals { get; set; }
  }
  </code></pre>
  
  <p>Association class:</p>
  
  <pre><code> public class ReservationMealItems
  {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int Id { get; set; }
  
      public int ReservationId { get; set; }
  
      public int MealId { get; set; }
  
  
      public virtual Reservation Reservation { get; set; }
      public virtual Meal Meal { get; set; }
  
  }
  </code></pre>
  
  <p>Meals class:</p>
  
  <pre><code>[Table("Meals")]
  public class Meal
  {
  
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int Id { get; set; }
      public string Name { get; set; }
      public string Description { get; set; }
      public string ImageUrl { get; set; }
      public int MealTypeId { get; set; }
  
  
      public virtual MealType Type { get; set; }
  
      public virtual ICollection&lt;MenuMealItem&gt; Menus { get; set; }
  
      // create association table ReservationMeal
      public virtual ICollection&lt;ReservationMealItems&gt; ReservationMeals { get; set; }
  
      // one-to-one with MealPrice where MealPrice is the dependant
      public virtual MealPrice MealPrice { get; set; }
  }
  </code></pre>
  
  <p>And here is my controller to which I am passing a Date and then querying the tables in order to bring back all the meals from all the reservations made on that particular date. </p>
  
  <pre><code>        public IHttpActionResult GetReservationMealsForDate(DateDto date)
      {
          using (var context = new CafeteriaContext())
          {
              var reservationMeals = from p in context.Reservations
                                     where p.Date == date.Date
                                     join d in context.ReservationMeals
                                     on p.ReservationId equals d.ReservationId
                                     select new SimpleId
                                     {
                                         Id = d.MealId
                                     };
  
  
              var meals = from p in context.Meals
                          join d in reservationMeals
                          on p.Id equals d.Id
                          select new SimpleMeal
                          {
                              Name = p.Name
                          };
  
  
              return Ok(meals.ToList());
          }
      }
  </code></pre>
  
  <p>The controller works and brings back the list of meals from all reservations made on that date but I am certain that it's not optimal. </p>
  
  <p>I tried not using the JOINs and instead make use of the navigation properties but with no success. I wonder if the relations are set up correctly and if the navigation properties are placed as they should be and if yes, then how can the controller be rewritten to bring back the same result set using navigation properties. </p>
  
  <p>And as a bonus question I would like to know how to write the <strong>correct</strong> query using LINQ method syntax as I did not succeed in writing it as such.</p>
  
  <p>I am currently learning and any explanations are greatly appreciated. I have read this blog post <a href="https://coding.abel.nu/2012/06/dont-use-linqs-join-navigate/" rel="nofollow noreferrer">Coding Abel</a> from where I managed to make it work. But I am still curious to know how correct my solution is and eager to learn better alternatives. Thanks!</p>