title: Filtering a Entity Framework Context to return only certain child elements
body: <p>Good day all, super newbie to ASP.NET, Entity Framework and the like.  Very versed in straight line programming, so I am first having to try and forget the old way before the new way can sink in.</p>
  
  <p>I am developing an app that will track a FLEET item (parent) and all of its FLEETEVENT items (child). The two are linked via a FleetId field.</p>
  
  <p>I can use: <code>Fleet fleet = db.Fleets.Find(id);</code> and it returns the parent record matching the id variable, and ALL of its child elements.</p>
  
  <p>What I am trying to do is to return ONLY the child elements where FLEETEVENT.STATUS == True</p>
  
  <p>I have tried using LINQ expressions and DataLoadOptions.AssosiateWith, but I get all records with the AssociateWith and conversion errors trying to use the LINQ expressions.</p>
  
  <p>I am thinking it should not be TOO hard to do this, but I am stuck in my old way of thinking.</p>
  
  <p>Code snipets are as follows:</p>
  
  <p>Connector model:</p>
  
  <pre><code>public class FleetContext : DbContext
  {
      public FleetContext() : base("DefaultConnection")
      {
      }
  
      public DbSet&lt;Fleet&gt; Fleets { get; set; }
      public DbSet&lt;FleetEvent&gt; FleetEvents { get; set; }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Configurations.Add(new FleetConfiguration());
          modelBuilder.Configurations.Add(new FleetEventConfiguration());
      }
  }
  </code></pre>
  
  <p>Parent model:</p>
  
  <pre><code>public class Fleet : IObjectWithState
  {
      public Fleet()
      {
          FleetEvents = new List&lt;FleetEvent&gt;();
          Status = true;
      }
  
      public System.Guid FleetId { get; set; }
      public string FleetType { get; set; }
      public string FleetNum { get; set; }
      public string FleetMake { get; set; }
      public string FleetModel { get; set; }
      public int FleetYear { get; set; }
  
      public bool Status { get; set; }
      public byte[] RowVersion { get; set; }
  
      public ObjectState ObjectState { get; set; }
  
      public virtual List&lt;FleetEvent&gt; FleetEvents { get; set; }
  }
  </code></pre>
  
  <p>Child model:</p>
  
  <pre><code>public class FleetEvent : IObjectWithState
  {
      public System.Guid FleetEventId { get; set; }
      public DateTime FleetEventDate { get; set; }
      public string FleetEventType { get; set; }
      public string FleetEventDesc { get; set; }
  
      public bool Status { get; set; }
      public byte[] RowVersion { get; set; }
  
      public System.Guid FleetId { get; set; }
      public Fleet Fleet { get; set; }
  
      public ObjectState ObjectState { get; set; }
  }
  </code></pre>
  
  <p>Using LINQ, this is the code in my controller that I am using:</p>
  
  <pre><code>var result = from f in db.Fleets
                       join fe in db.FleetEvents
                           on f.FleetId equals fe.FleetId
                       where ((f.FleetId == id) &amp;&amp; (fe.Status == true))
                       select new { f, fe };
  
          Fleet fleet = result.ToList();
  </code></pre>
  
  <p>I have verified that the LINQ syntax is correct via LinqPad returning the results I expected.  But when I use it in code, I get conversion errors in the Fleet fleet line of code.</p>
  
  <p>Any information to point me in the right direction would be greatly appreciated.</p>