title: Removing Elements From One List If not contained in another List. C#
body: <p>What is the best way for me to remove an element from <code>List&lt;&gt; A</code> that is not in <code>List&lt;&gt; B</code>. On Top of this before I remove the <code>Element</code> I need to run a function on that <code>Element</code> (Callback is run when value changed).</p>
  
  <p>For Example</p>
  
  <pre><code>List&lt;Chunk&gt; ActiveChunks = //List Of Active Chunks.
  void UpdateChunks()
  {
     List&lt;Chunk&gt; chunks = //Chunks To Activate
     if (ActiveChunks[Element] is not Contained In chunks){
        A.Active = False
        A.Remove[Element]
     }
     //After Removing Chunks That do not exist in "chunks" add from chunks to ActiveChunks What is not In already in Chunks
  } 
  </code></pre>
  
  <p>What My Code Looks Like</p>
  
  <pre><code>private List&lt;Chunk&gt; ActiveChunks = new List&lt;Chunk&gt;();
  private void OnPlayerMoved(Character c)
  {
      //Current chunk That Player is in
      List&lt;Chunk&gt; chunks = new List&lt;Chunk&gt;();
      for (int i = -(GameManager.ViewRange - 1); i &lt; GameManager.ViewRange - 1; i++)
      {
          Chunk chunk = world.ChunkAtWorldPos(c.PosX + i, c.PosY + i);
          if (chunk != null)
              chunks.Add(chunk);
      }
      if (ActiveChunks.Count &gt; 0)
      {
          foreach (Chunk chunk in ActiveChunks)
          {
              if (chunks.Contains(chunk) == false)
              {
                  chunk.Active = false;
              }
          }
          //This Line From Answer down Below
          ActiveChunks.RemoveAll(x =&gt; x.Active == false);
          foreach (Chunk chunk in chunks)
          {
              if (ActiveChunks.Contains(chunk) == false)
              {
                  chunk.Active = true;
                  ActiveChunks.Add(chunk);
              }
          }
      }
      else
          ActiveChunks.AddRange(chunks);
  }
  
  public class Chunk
      {
          //Position
          public readonly int X;
          public readonly int Y;
  
          public Tile[,] Tiles;
  
          private bool active = false;
          public bool Active{
              get{return active;}
              set{
                  if (value == true &amp;&amp; value != active)
                      cbOnActivate?.Invoke(this);
                  else if (value != active)
                      cbOnDeactivate?.Invoke(this);
  
                  active = value;
              }
          }
  
          Action&lt;Chunk&gt; cbOnActivate;
          Action&lt;Chunk&gt; cbOnDeactivate;
  
          //Constructor
          public Chunk(int X, int Y)
          {
              this.X = X;
              this.Y = Y;
          }
  
          void RegisterOnActivateCallback(Action&lt;Chunk&gt; callback){
              cbOnActivate += callback;
          }
          void RegisterOnDeactivateCallback(Action&lt;Chunk&gt; callback){
              cbOnActivate += callback;
          }
      }
  </code></pre>