title: C# foreach on children of an abstract
body: <p>I'm new to C# and my question is:<br>
  I have a class named </p>
  
  <blockquote>
    <p>AbstractItem</p>
  </blockquote>
  
  <p>and there are 3 children to this class.
  My code is:<br></p>
  
  <pre><code>private void AddChecker(List&lt;AbstractItem&gt; getByChosenField)
      {
          if (getByChosenField == null)
          {
              MessageBox.Show("Entry list is currently empty.");
          }
          else {
              LibraryList.Items.Clear(); // simply if there are no results then list remains empty.
              if (getByChosenField.OfType&lt;Reading&gt;().Count() &gt; 0)
              {
                  foreach (Reading item in getByChosenField)
                  {
                      LibraryList.Items.Add(
                          new MyItems
                          {
                              ItemName = item.ItemName,
                              CopyNumber = int.Parse(item.CopyNumber.ToString()),
                              Guid = int.Parse(item.Guid.ToString()),
                              TimePrinted = item.Time,
                              BestSeller = item.BestSeller,
                              Category = item.BookCategory.ToString(),
                              SubCategory = item.ReadingBookSubCategory.ToString()
                          });
                  }
              }
              if (getByChosenField.OfType&lt;Cooking&gt;().Count() &gt; 0)
              {
                  foreach (Cooking item in getByChosenField)
                  {
                      LibraryList.Items.Add(
                          new MyItems
                          {
                              ItemName = item.ItemName,
                              CopyNumber = int.Parse(item.CopyNumber.ToString()),
                              Guid = int.Parse(item.Guid.ToString()),
                              TimePrinted = item.Time,
                              BestSeller = item.BestSeller,
                              Category = item.BookCategory.ToString(),
                              SubCategory = item.CookingBookSubCategory.ToString()
                          });
                  }
              }
              if (getByChosenField.OfType&lt;Science&gt;().Count() &gt; 0)
              {
                  foreach (Science item in getByChosenField)
                  {
                      LibraryList.Items.Add(
                          new MyItems
                          {
                              ItemName = item.ItemName,
                              CopyNumber = int.Parse(item.CopyNumber.ToString()),
                              Guid = int.Parse(item.Guid.ToString()),
                              TimePrinted = item.Time,
                              BestSeller = item.BestSeller,
                              Category = item.BookCategory.ToString(),
                              SubCategory = item.ScienceBookSubCategory.ToString()
                          });
                  }
              }
              if (getByChosenField.OfType&lt;Journal&gt;().Count() &gt; 0)
              {
                  foreach (Journal item in getByChosenField)
                  {
                      LibraryList.Items.Add(
                          new MyItems
                          {
                              ItemName = item.ItemName,
                              CopyNumber = int.Parse(item.CopyNumber.ToString()),
                              Guid = int.Parse(item.Guid.ToString()),
                              TimePrinted = item.Time,
                              Category = "Journal",
                              SubCategory = item.JournalCategory.ToString()
                          });
                  }
              }
          }
      }
  </code></pre>
  
  <p>What I'm trying to do is getting all of the values inside this List that matches getByChosenField.<br>
  Now, GetByChosenField in my case is a <strong>BookName</strong> that returns all of the books in the list that are currently with the <strong>BookName</strong> name. 
  <br>
  The code that does the searching is indeed working.<br>
  The problem is, if I have a book name <strong>Harry Potter</strong> inside <strong>Cooking</strong> and also <strong>Reading</strong> and\or <strong>Science</strong>, I get an error: <br></p>
  
  <blockquote>
    <p>Additional information: Unable to cast object of type 'CommonBookLib.Cooking' to type 'CommonBookLib.Reading'.</p>
  </blockquote>
  
  <p>This error occures only when I have at least 2 book names of other Categories (Reading\Cooking\Science\Journal).<br>
  In case of one book name, everything is fine.<br>
  What am I missing here?</p>