title: How to fill a list collection from a database, by WPF combobox selection
body: <p>I have Many-to-Many Tables and want to display some of the fields in WPF controls.</p>
  
  <p><img src="https://i.stack.imgur.com/fyqG9.png" alt="enter image description here"></p>
  
  <p>How can I fill the list collection <code>List&lt;Course&gt; courses = new List&lt;Course&gt;()</code> whenever I select the InstructorIDs in the combobox ? I want to display only the CourseID and Title from the List in the ListView control</p>
  
  <p>Here're my codes:</p>
  
  <pre><code>public partial class MainWindow : Window
  {
      SchoolEntities db = new SchoolEntities();
      public MainWindow()
      {
          InitializeComponent();
          var instructors = db.Instructors.Where(f =&gt; f.HireDate == 2011).ToList();
  
          this.comboBox1.ItemsSource = instructors;
          this.comboBox1.DisplayMemberPath = "LastName";
          this.comboBox1.SelectedValuePath = "InstructorID";           
      }
      List&lt;Course&gt; courses = new List&lt;Course&gt;();
  
      private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
      {
          int S = (int)this.comboBox1.SelectedValue;
          var InstrSelection = db.Instructors.Include("CourseInstructors.Course").SingleOrDefault(f =&gt; f.InstructorID == S);
  
  
          foreach (var C in InstrSelection.CourseInstructors)
          {
              courses.Add(C.Course);
          }
  
          this.listView1.DataContext = null;
          this.listView1.DataContext = courses;
      }
  }   
  </code></pre>
  
  <p>And the window xaml</p>
  
  <pre><code>&lt;Window x:Class="School.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="773" Width="677"&gt;
  
  &lt;Grid&gt;        
      &lt;ComboBox Height="23" HorizontalAlignment="Left" Name="comboBox1"  SelectionChanged="comboBox1_SelectionChanged"&gt;&lt;/ComboBox&gt;
      &lt;ListView  HorizontalAlignment="Left" Name="listView1"&gt;
          &lt;ListView.View&gt;
           &lt;GridView&gt;
              &lt;GridViewColumn Width="140" Header= "CourseID" /&gt;
              &lt;GridViewColumn Width="140" Header= "Title" /&gt;                
           &lt;/GridView&gt;
          &lt;/ListView.View&gt;
      &lt;/ListView&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>