title: Use LINQ to compare two lists, and produce a third one with results from one OR the other
body: <p>I have two lists of elements that differ only on the <code>IsTemporalPaymentTerm</code> boolean field.</p>
  <p>I want to use LINQ to compare the two lists, entry by entry, and produce a third list that has, for each entry, the one where <code>IsTemporalPaymentTerm = true</code>, and <strong>if there isn't one</strong>, then I want the one where <code>IsTemporalPaymentTerm = false</code>.</p>
  <p>Here's some sample code:</p>
  <pre><code>var allResults = db.PaymentTerms
                         .AsQueryable()
                         .Where(y =&gt; y.WorkDate == date
                                 &amp;&amp; y.ProjectID == ProjectID 
                                 &amp;&amp; y.CompanyID == CompanyID
                                 &amp;&amp; y.PayeeID == PayeeID);
  
  //TABLE WITH ONLY TEMPORAL PAYMENT TERMS
  var onlyTemporalResults = allResults.Where(x =&gt; x.IsTemporalPaymentTerm);
  
  //TABLE WITH ONLY NON-TEMPORAL PAYMENT TERMS
  var nonTemporalResults = allResults.Where(x =&gt; !x.IsTemporalPaymentTerm);
  </code></pre>
  <p>So, basically what I want is to compare <code>onlyTemporalResults</code> against <code>nonTemporalResults</code> , and get a final list that has either the temporal payment term, <strong>OR</strong> the non-temporal payment term <strong>if no temporal payment term could be found.</strong></p>
  <p>Pseudo code example:</p>
  <pre><code>List&lt;PaymentTerms&gt; TemporalPaymentTerms = new List&lt;PaymentTerms&gt;();
  PaymentTerm unnaprovedPT1 = new PaymentTerm  { PayeeID = 1, CompanyID = 2, ProjectID = 3, IsTemporalPaymentTerm = false };
  PaymentTerm unnaprovedPT2 = new PaymentTerm  { PayeeID = 2, CompanyID = 2, ProjectID = 3, IsTemporalPaymentTerm = false };
  
  TemporalPaymentTerms.Add(unnaprovedPT1); 
  TemporalPaymentTerms.Add(unnaprovedPT2);  
     
  List&lt;PaymentTerms&gt; NonTemporalPaymentTerms = new List&lt;PaymentTerms&gt;();
  PaymentTerm approvedPT1 = new PaymentTerm  { PayeeID = 2, CompanyID = 2, ProjectID = 3, IsTemporalPaymentTerm = true};
  PaymentTerm approvedPT1 = new PaymentTerm  { PayeeID = 3, CompanyID = 2, ProjectID = 3, IsTemporalPaymentTerm = true};
  
  //LINQ query that merges both lists goes here.
      
  //FINAL EXPECTED RESULT:
  List&lt;PaymentTerms&gt; FinalList = [
  {PayeeID = 1, CompanyID = 2, ProjectID = 3, IsTemporalPaymentTerm = false},
  {PayeeID = 2, CompanyID = 2, ProjectID = 3, IsTemporalPaymentTerm = false},
  {PayeeID = 3, CompanyID = 2, ProjectID = 3, IsTemporalPaymentTerm = true}
  ];
  </code></pre>
  <p>I know this can be done iterating over the two lists (temporal and non-temporal Payment Terms), and then comparing them, but I guess my question is:</p>
  <p>Can this be done, more efficiently and in a more elegant way, using a single LINQ query? Maybe a certain form of join that I am missing? Conditional Where clauses?</p>
  <p>So far I have failed to see the answer. Thanks in advance!</p>