title: How to properly extend a WebAPI 2 OData v4 controller to automatically provide related entity collections
body: <p>In my WebAPI 2 / Entity Framework 6 / OData v4 service, I have the following simple controller:</p>
  
  <pre><code>public class InformationProductController : ODataController
      {
          GCIMContext db = new GCIMContext();
  
          protected override void Dispose(bool disposing)
          {
              db.Dispose();
              base.Dispose(disposing);
          }
  
          [EnableQuery]
          public IQueryable&lt;InformationProduct&gt; Get()
          {
              return db.InformationProducts;
          }
      }
  </code></pre>
  
  <p>My InformationProduct entity has a collection of child entities of type <code>DataEntity</code>:</p>
  
  <pre><code>public partial class InformationProduct
      {
          public InformationProduct()
          {
              this.AnalyticalMethods = new List&lt;AnalyticalMethod&gt;();
              this.DataEntities = new List&lt;DataEntity&gt;();
              this.BusinessEntities = new List&lt;BusinessEntity&gt;();
              this.SourceTools = new List&lt;SourceTool&gt;();
          }
  
          public int ID { get; set; }
          public string Name { get; set; }
          public string Description { get; set; }
          public Nullable&lt;int&gt; Governance_ID { get; set; }
          public Nullable&lt;int&gt; PerformanceMetric_ID { get; set; }
          public virtual ICollection&lt;AnalyticalMethod&gt; AnalyticalMethods { get; set; }
          public virtual ICollection&lt;DataEntity&gt; DataEntities { get; set; }
          public virtual Governance Governance { get; set; }
          public virtual PerformanceMetric PerformanceMetric { get; set; }
          public virtual ICollection&lt;BusinessEntity&gt; BusinessEntities { get; set; }
          public virtual ICollection&lt;SourceTool&gt; SourceTools { get; set; }
      }
  </code></pre>
  
  <p>The DataEntity, in turn, has a collection of child entities of type <code>DataSource</code>:</p>
  
  <pre><code>public partial class DataEntity
      {
          public DataEntity()
          {
              this.PerformanceMetrics = new List&lt;PerformanceMetric&gt;();
              this.DataAttributes = new List&lt;DataAttribute&gt;();
              this.BusinessEntities = new List&lt;BusinessEntity&gt;();
              this.DataDeliveryChannels = new List&lt;DataDeliveryChannel&gt;();
              this.DataSources = new List&lt;DataSource&gt;();
              this.MasterDatas = new List&lt;MasterData&gt;();
              this.SourceTools = new List&lt;SourceTool&gt;();
              this.SubjectAreas = new List&lt;SubjectArea&gt;();
              this.Udms = new List&lt;Udm&gt;();
          }
  
          public int ID { get; set; }
          public string Name { get; set; }
          public string Description { get; set; }
          public Nullable&lt;int&gt; InformationProduct_ID { get; set; }
          public Nullable&lt;int&gt; BiMeasure_ID { get; set; }
          public Nullable&lt;int&gt; BiFact_ID { get; set; }
          public Nullable&lt;int&gt; BiDimension_ID { get; set; }
          public virtual BiDimension BiDimension { get; set; }
          public virtual BiFact BiFact { get; set; }
          public virtual BiMeasure BiMeasure { get; set; }
          public virtual InformationProduct InformationProduct { get; set; }
          public virtual ICollection&lt;PerformanceMetric&gt; PerformanceMetrics { get; set; }
          public virtual ICollection&lt;DataAttribute&gt; DataAttributes { get; set; }
          public virtual ICollection&lt;BusinessEntity&gt; BusinessEntities { get; set; }
          public virtual ICollection&lt;DataDeliveryChannel&gt; DataDeliveryChannels { get; set; }
          public virtual ICollection&lt;DataSource&gt; DataSources { get; set; }
          public virtual ICollection&lt;MasterData&gt; MasterDatas { get; set; }
          public virtual ICollection&lt;SourceTool&gt; SourceTools { get; set; }
          public virtual ICollection&lt;SubjectArea&gt; SubjectAreas { get; set; }
          public virtual ICollection&lt;Udm&gt; Udms { get; set; }
      }
  </code></pre>
  
  <p>The following OData query runs fine in Fiddler, Postman, and any modern browser:</p>
  
  <pre><code>GET http://10.0.0.4:8080/InformationProduct?$expand=DataEntities($expand=DataSources)
  </code></pre>
  
  <p>The result of this query is:</p>
  
  <pre><code>{
    "@odata.context":"http://10.0.0.4:8080/$metadata#InformationProduct","value":[
      {
        "ID":1,"Name":"ODM Dashboard","Description":"ODM Dashboard","Governance_ID":1,"PerformanceMetric_ID":1,"DataEntities":[
          {
            "ID":1,"Name":"Data Entity 1","Description":"Data Entity 1","InformationProduct_ID":1,"BiMeasure_ID":null,"BiFact_ID":null,"BiDimension_ID":1,"DataSources":[
              {
                "ID":40,"Category":"Service Performance","SourceSystemName":"Account Improvement Plan","SourceSystemOwner":null,"SourceSystemLocation":null,"SourceSystemTeam":null,"SourceSystemNetworkSegment":null,"SourceSystemOsType":null,"SourceDatabaseName":null,"SourceDatabaseType":null,"SourceDatabaseVersion":null,"BiFact_ID":null
              }
            ]
          }
        ]
      }
    ]
  }
  </code></pre>
  
  <p>Since I was unable to find any JavaScript libraries, which can express a nested <code>$expand</code> operator, I am now turning to my API, and would like it to supply the <code>InformationModel</code> collection, along with expanded <code>DataEntities</code> collection, which would come along with its own expanded <code>DataSources</code> collection - exactly like in the query shown above.</p>
  
  <p>My question is:  What syntax should I use to extend my IQueryable result to include both <code>DataEntities</code>, and its <code>DataSources</code> collection?</p>