title: Is it possible to follow a chain of Foreign Key references in a single query?
body: <p>I have a database with two (relevant) tables: <code>Actions</code> and <code>Documents</code> and am querying it using LINQ.</p>
  
  <p>An Action points to its input Document and a Document points back to the Action that created it, so <code>Action.Document</code> and <code>Document.SourceAction</code>. </p>
  
  <p>By following back these relations we get a processing chain of alternating actions and documents.</p>
  
  <pre><code>Final Document-&gt; Last Action-&gt; Middle Document-&gt; First Action-&gt; Source Document
  </code></pre>
  
  <p>When a document doesn't have a source action, it is the original source document.</p>
  
  <p>I'm using the following query in LINQPad to gather all the actions in such a chain and output them:</p>
  
  <pre><code>var actions = new List&lt;LINQPad.User.Action&gt;();
  var action = Actions.Where(a =&gt; a.Id == 1240).First();
  while (action != null) {
      actions.Add(action);
      action = action.Document != null ? action.Document.SourceAction : null;
  }
  actions.Dump();
  </code></pre>
  
  <p>This query works fine, but it of course fires a SQL query for every action.</p>
  
  <p>Is there a way to get all these actions in a single LINQ query?</p>