title: Unable to cast object of type 'Model.TreeModel' to type 'ViewModels.TreeViewModel'
body: <p>I am trying to create objects from another object which has a list of objects. But I am getting a casting error on the line where I do the casting. Is there a way to cast object of different collection on to another collection.</p>
  
  <p>Code:</p>
  
  <pre><code>public List&lt;TreeViewModel&gt; getAllTreeNodesFromModel()
  {
      treeNodeViewModel = treeModel.getTreeNodes().Select(a =&gt; new TreeViewModel
        {
          Children = a.Children.Select(c =&gt; new TreeViewModel{Value = c.Value}).ToList(),
          Value = a.Value,
         }).ToList();
  
        return treeNodeViewModel;
    }
  
  // Making list observable
  public ObservableCollection&lt;TreeViewModel&gt; TreeView
  {
      get
      {
          return treeView; 
      }
  
      set
      {
          if (treeView == value) return;
          treeView = value;
          NotifyPropertyChanged("TreeView");
      }
  }
  </code></pre>
  
  <p>XAML:</p>
  
  <pre><code>&lt;TreeView Margin="644,137,6,6" Grid.RowSpan="2" ItemsSource="{Binding TreeView}"&gt;
     &lt;TreeView.Resources&gt;
         &lt;HierarchicalDataTemplate DataType="{x:Type local:MainWindowModel}" ItemsSource="{Binding Children}"&gt;
             &lt;CheckBox IsChecked="{Binding Value}" /&gt;
          &lt;/HierarchicalDataTemplate&gt;
      &lt;/TreeView.Resources&gt;
  &lt;/TreeView&gt;
  </code></pre>
  
  <p>Here is my model:</p>
  
  <pre><code>public class TreeModel
  {
      public string Name { get; set; }
      public List&lt;TreeModel&gt; Children { get; set; }
      public string Value { get; set; }
  
      public TreeModel()
      {
          Children = new List&lt;TreeModel&gt;();
      }
  
      public TreeModel(string name, List&lt;TreeModel&gt; children)
      {
  
          this.Name = name;
          this.Children = children;
      }
  
      public List&lt;TreeModel&gt; BuildTree(IEnumerable&lt;string&gt; strings)
      {
          return (
            from s in strings
            let split = s.Split('.')
            group s by s.Split('.')[0] into g  // Group by first component (before /)
            select new TreeModel
            {
                Value = g.Key,
                Children = BuildTree(            // Recursively build children
                  from s in g
                  where s.Length &gt; g.Key.Length + 1
                  select s.Substring(g.Key.Length + 1)) // Select remaining components
            }
            ).ToList();
      }
  
      // View Model
      public string Value { get; set; }
      public  List&lt;TreeViewModel&gt; Children { get; set; }
  
      // List VM mapped from Model
      public List&lt;TreeViewModel&gt; getAllTreeNodesFromModel()
      {
           treeNodeViewModel = treeModel.getTreeNodes().Select(a =&gt; new TreeViewModel
           {
               Children = a.Children.Cast&lt;TreeViewModel&gt;().ToList(),
               Value = a.Value,
  
           }).ToList();
  
           return treeNodeViewModel;
       }
  </code></pre>
  
  <p>XAML:</p>
  
  <pre><code>&lt;TreeView Margin="644,137,6,6" Grid.RowSpan="2" ItemsSource="{Binding TreeView}"  &gt;
      &lt;TreeView.Resources&gt;
          &lt;HierarchicalDataTemplate DataType="{x:Type local:MainWindowModel}"  ItemsSource="{Binding Children}"&gt;
              &lt;CheckBox IsChecked="{Binding Value}" /&gt;
          &lt;/HierarchicalDataTemplate&gt;
      &lt;/TreeView.Resources&gt;
  &lt;/TreeView&gt;
  </code></pre>