title: How to compare selected items of listbox to those in database and delete the ones that aren't selected
body: <p>I'm having a bit of trouble getting my code to work. I'm retrieving all of my actor objects from my database, which are the castmembers of a certain movie, using LINQ to SQL. I've checked the output and they're just fine.</p>
  
  <p>Because I had to use a dictionary to show the actors names as a whole, I need to compare whether the selected values of the listbox are the same as the combination of the First- and LastName of the actor.</p>
  
  <p>The first thing I'm doing is going through all the selected items in my listbox. Next, I want to compare them to the First and LastName of the actorsm which I retrieved using LINQ. But it doesn't work like I want it to.</p>
  
  <p>Example: Movie is Pulp Fiction and the Actors related to the movie are Uma Thurman, Samuel L. Jackson and John Travolta. When I run this code I get a total of 3 MessageBoxes with the following output:</p>
  
  <ul>
  <li>[4, Uma Thurman] Matches Uma Thurman </li>
  <li>[5, Samuel L. Jackson] Matches    Uma Thurman</li>
  <li>[6, John Travolta] Matches Uma Thurman</li>
  </ul>
  
  <p>Though what I really want to achieve is that it breaks out of the loop after finding Uma and then get a messagebox saying:</p>
  
  <p>[5, Samuel L. Jackson] Matches Samuel L. Jackson. and so forth. I want to do this since I want to delete and/or insert actors that are selected/deselected from the movie.</p>
  
  <pre><code>var deleteMovieActors = db.MovieActors.Where(mid =&gt; mid.Movie_MovieId == MovieId).Select(a =&gt; a.Actor);
  
          foreach (var item in lstbActors.SelectedItems)
          {
              foreach (Actor a in deleteMovieActors)
              {
                  if (lstbActors.SelectedItem.ToString().Contains(a.FirstName + " " + a.LastName))
                  {
                      MessageBox.Show(item.ToString() + " Matches " + a.FirstName + " " + a.LastName);
                      break;
                  } 
              }                        
          }
  </code></pre>
  
  <p>Here's how I create my listbox:</p>
  
  <pre><code>private void InitializeListBox()
      {
          var actors = new Dictionary&lt;int, string&gt;();
  
          foreach (var actor in db.Actors.ToList())
          {
              actors.Add(actor.ActorId, actor.FirstName + " " + actor.LastName);
          }
  
          lstbActors.DataSource = actors.ToList();
          lstbActors.ValueMember = "Key";
          lstbActors.DisplayMember = "Value";
          lstbActors.SelectedIndex = -1;
  
          // Get Actors by MovieId
          var MAct = from ma in db.MovieActors
                     where ma.Movie_MovieId == MovieId
                     select ma;
  
          List&lt;Actor&gt; act = new List&lt;Actor&gt;();
  
          foreach (var ma in MAct)
          {
              act.Add(db.Actors.Where(a =&gt; a.ActorId == ma.Actor_ActorId).Single());
          }
  
          foreach (Actor a in act)
          {
              int index = lstbActors.FindStringExact(a.FirstName + " " + a.LastName);
              if (index != -1)
              {
                  lstbActors.SetSelected(index, true);
              }
          }
      }
  </code></pre>