title: How to combine 2 lists and add new ones using LINQ?
body: <p>There are two lists. I want to combine both lists based on key (property) Id and update this records with values from list 2. All list-items that are not in list one must be added.</p>
  
  <p>I searched the internet and found information about the LINQ join functionality, but I want also add new records/items to the list using LINQ. Does anyone know how to do that (using 1 LINQ query)? </p>
  
  <p>My code so far.</p>
  
  <pre><code>public class Item
  {
      public string Id { get; set; }
      public string Name { get; set; }
      public string Value { get; set; }
  }
  
  [TestFixture]
  public class CompareListTest
  {
      private List&lt;Item&gt; firstList;
  
      [SetUp]
      public void SetUp()
      {
          firstList.Add(new Item()
          {
              Id = "1",
              Name = "Name1",
              Value = "NotUpdated1"
          });
      }
  
      [Test]
      public void ShouldCombineTwoListsAndAddNewOnesUsingIdAsPrimaryKey()
      {
          List&lt;Item&gt; secondList = new List&lt;Item&gt;();
          secondList.Add(new Item()
          {
              Id = "1",
              Name = "NameUpdated1",
              Value = "Updated1"
          });
          secondList.Add(new Item()
          {
              Id = "2",
              Name = "NewOne",
              Value = "New"
          });
  
          var combineList = new CombineList();
          List&lt;Item&gt; combinedList = combineList.Update(firstList, secondList);
          Assert.That(combinedList.Count, Is.EqualTo(2));
          Assert.That(combinedList.FirstOrDefault(x =&gt; x.Id == "1").Value, Is.EqualTo("Updated1"));
          Assert.That(combinedList.FirstOrDefault(x =&gt; x.Id == "2").Value, Is.EqualTo("New"));
      }
  }
  
  public class CombineList
  {
      public List&lt;Item&gt; Update(List&lt;Item&gt; firstList, List&lt;Item&gt; secondList)
      {
          var result = from x in firstList
                       join y in secondList
                       on x.Id equals y.Id
                       select new Item()
                       {
                           Id = y.Id,
                           Name = y.Name,
                           Value = y.Value
                       };
          return result.ToList();
      }
  }
  </code></pre>