title: Linq Select: Using a value from a list if available
body: <p>I am going to use some basic stripped down examples to illustrate my problem.</p>
  
  <p>I have a class:</p>
  
  <pre><code>class Item
  {
     int ID;
     bool Selected;
  }
  </code></pre>
  
  <p>Now lets say I have two lists of the <code>Item</code> class:</p>
  
  <pre><code>List&lt;Item&gt; ListA = GetListA();
  List&lt;Item&gt; ListB = GetListB();
  </code></pre>
  
  <p>Now I want to create a third list that has all the items from <code>ListB</code>. The important thing is that if a match (same <code>ID</code>) if found in <code>ListA</code> then I want to use that <code>Selected</code> value, otherwise I want to keep the <code>Selected</code> value of the item that is in <code>ListB</code>.</p>
  
  <p>I am creating the third list as follows:</p>
  
  <pre><code>List&lt;Item&gt; ListC = from item in ListB 
  select new Item
  {
     ID = item.ID,
     Selected = item.Selected// &lt;-- should use value form ListA if available
  };
  </code></pre>
  
  <p><strong>Important</strong>: I don't want to seem ignorant, but I <strong>do not</strong> want to change the way <code>ListC</code> is created. By that I mean I want to use the "linq select" method, and I want to use a "one liner" that assigns the <code>Selected</code> value... I know there are other ways to create the list which will work just fine, but then I won't learn anything new.</p>
  
  <hr>
  
  <p>I have tried a couple of things so far...</p>
  
  <p>I know this will work, but I don't want to query <code>ListA</code> twice:</p>
  
  <pre><code>Selected = ListA.Any(x =&gt; x.ID == item.ID) ? ListA.First(x =&gt; x.ID == item.ID).Selected : item.Selected
  </code></pre>
  
  <p>and I also tried using <code>DeafultIfEmpty</code> but I don't think that is the right think for this situation... because it didn't work, and it seems it is more use if <code>ListA</code> was empty (which I don't care about)</p>