title: C# LINQ join - use range variable in secondary join
body: <p>I am trying to use some linq magic to query over two different data sets to compare an inner property on one data set to see if that value is greater than a property in the other data set. I've generalized the class layouts and removed some properties to hopefully make it clearer.</p>
  
  <pre><code>public class Contract
  {
      public string ContractId { get; set; }
      public IList&lt;Invoice&gt; InvoiceList { get; set; }
  
      public class Invoice
      {
          public int InvoiceNumber { get; set; }
          public decimal CurrentDueAmount { get; set; }
      }
  }
  
  public class PaymentRequest
  {
      public IList&lt;ContractList&gt; Contracts { get; set; }
      public class ContractList
      {       
          public string ContractId { get; set; }  
          public IList&lt;InvoiceList&gt; Invoices { get; set; }
      }
  
      public class InvoiceList
      {          
          public decimal CurrentDueAmount { get; set; }
      }
  }
  </code></pre>
  
  <p>I know the following LINQ statement doesn't work but its the general idea on what I'm trying to achieve.</p>
  
  <pre><code>from validContract in validContracts
  join contract in paymentRequest.Contracts
  on validContract.ContractId equals contract.ContractId
  
  from validInvoice in validContract.InvoiceList
  join invoice in contract.Invoices
  on validInvoice.InvoiceNumber equals invoice.InvoiceNumber
  
  where invoice.CurrentDueAmount &gt; validInvoice.CurrentDueAmount
  select "Invoice Number: " + invoice.InvoiceNumber + ", Current Due &gt;= " + validInvoice.CurrentDueAmount;
  </code></pre>
  
  <p>I get an error trying to use the range variable "contract" in the second join statement. 
  <a href="https://i.stack.imgur.com/UGSu8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UGSu8.png" alt="enter image description here"></a></p>
  
  <p>I'd like to be able to do the comparison on the objects currentDueAmounts. Any ideas / refactoring, method extractions, etc. would be greatly appreciated. </p>