title: Improving efficiency with lazy/eager loading using a virtual (foreign key) reference
body: <p>I am relatively new to Lambda/Linq, however I want to retrieve all events from a specific calendar where the events are still in the future...</p>
  
  <p>If I use:</p>
  
  <pre><code>EventCalendar eventCalendar;
  eventCalendar = db.Events_Calendars.Find(id);
  </code></pre>
  
  <p>I can get all events and filter by the current date in the view, but I don't think that is the best method.</p>
  
  <p>The Model is as follows:</p>
  
  <pre><code>[Table("Events_Calendars")]
  public class EventCalendar
  {
      public int Id { get; set; }
      public string Calendar { get; set; }
      public virtual List&lt;Event&gt; Events { get; set; }
  }
  </code></pre>
  
  <p>Event Model is:</p>
  
  <pre><code>public class Event
  {
      public int Id { get; set; }
      public string Title { get; set; }
      public string Description { get; set; }
      public DateTime Start { get; set; }
      public DateTime End { get; set; }
      public int? Capacity { get; set; }
      .
      .
      .
  }
  </code></pre>
  
  <p>The View is:</p>
  
  <pre><code>@model WebApps.Areas.Events.Models.EventCalendar
  @* does some stuff with the EventCalendar *@
  
  @Html.Partial("_Events", Model.Events.ToList())
  </code></pre>
  
  <p>_Events partial view:</p>
  
  <pre><code>@model IEnumerable&lt;WebApps.Areas.Events.Models.Event&gt;
  .        
  . 
  . 
  @foreach (var item in Model)
      {
          if (item.End &gt; DateTime.Today)
          {
              @* Do that thing *@
          }    
      }
  </code></pre>
  
  <p>The above example works, but is putting logic flow in the view. I want to remove the filter logic from the view and make the query do all the work.</p>
  
  <p>The following gives me the calendar only if it has at least one future event which is due to the same where clause conditions. I left it in as a reference of what I need to filter, yet it still returns all events both past and future: </p>
  
  <pre><code>eventCalendar = db.Events_Calendars
          .Where(x =&gt; x.Events.Any(y =&gt; y.End &gt;= DateTime.Today) &amp;&amp; x.Id == id)
          .FirstOrDefault();
  </code></pre>
  
  <p>Is there a way that I can filter the events when I ask for the calendar and still use only one statement?</p>
  
  <p>Note: Eager loading is defaulted to on with this system.</p>
  
  <p>Edit clarification =  return "no more than 1 calendar with all associated future events";</p>