title: Linq select list from ObservableCollection with different models in WPF
body: <p><strong>Scenario:</strong> I have two Datagrid which bind to <code>ObservableCollection&lt;Product&gt; ProductList</code> and <code>ObservableCollection&lt;ProductQuantity&gt; QuantityList</code>. I would like to select and add <code>List&lt;ProductQuantity&gt; Stock</code> from <code>ObservableCollection&lt;Product&gt; ProductList</code> where ProductList.id == SelectedProduct.id to new <code>ObservableCollection&lt;ProductQuantity&gt; QuantityList</code>. So whenever user select a row from ProductList, QuantityList will be updated based on the select row.</p>
  <p><strong>Model (Product.cs)</strong></p>
  <pre><code>public class Product : ObservableObject
  {
      public int Id { get; set; }
  
      public string Product_no { get; set; }
  
      public List&lt;ProductQuantity&gt; Stock { get; set; }
  }
  
  public class ProductQuantity : ObservableObject
  {
      public string Branch_name { get; set; }
  
      public float Quantity { get; set; }
  }
  </code></pre>
  <p><strong>ViewModel (ProductViewModel.cs)</strong></p>
  <pre><code>public class ProductViewModel : ViewModelBase
  {
      public ProductViewModel()
      {
          searchCommand = new RelayCommand(Search);
  
          SelectedProduct = new Product();
      }
  
      #region Define
      private ObservableCollection&lt;Product&gt; productList;
      public ObservableCollection&lt;Product&gt; ProductList
      {
          get { return productList; }
          set { productList = value; OnPropertyChanged(&quot;ProductList&quot;); }
      }
  
      private ObservableCollection&lt;ProductQuantity&gt; quantityList;
      public ObservableCollection&lt;ProductQuantity&gt; QuantityList
      {
          get { return quantityList; }
          set { quantityList = value; OnPropertyChanged(&quot;QuantityList&quot;); }
      }
  
      private Product selectedProduct;
      public Product SelectedProduct
      {
          get { return selectedProduct; }
          set
          { 
              selectedProduct = value;
              OnPropertyChanged(&quot;SelectedProduct&quot;);
              LoadProductQuantity();
          }
      }
      #endregion
  
  
      #region CustomOperation
      private void LoadProductQuantity()
      {
          if (ProductList != null)
          {
              // here
              QuantityList = new ObservableCollection&lt;ProductQuantity&gt;((List&lt;ProductQuantity&gt;)ProductList.Where(e =&gt; e.Id == SelectedProduct.Id).Select(s =&gt; s.Stock));
          }
      }
      #endregion
  
  
      #region SearchOperation
      private RelayCommand searchCommand;
      public RelayCommand SearchCommand
      {
          get { return searchCommand; }
      }
      private async void Search()
      {
          try
          {
              
              RootProductObject Response = await GetProductListAPI();
                  
  
              //datagrid
              ProductList = new ObservableCollection&lt;Product&gt;(Response.Data.OrderBy(property =&gt; property.Id));
              
          }
          catch (Exception e)
          {
              //
          }
      }
      #endregion
  </code></pre>
  <p><strong>Json from backend API (express.js)</strong></p>
  <pre><code>&quot;data&quot;: [
      {
          &quot;id&quot;: 1,
          &quot;product_no&quot;: &quot;a000001&quot;,
          &quot;stock&quot;: [
              {
                  &quot;branch_name&quot;: &quot;US Branch&quot;,
                  &quot;quantity&quot;: 3.1
              },
              {
                  &quot;branch_name&quot;: &quot;India Branch&quot;,
                  &quot;quantity&quot;: 3.1
              },
              {
                  &quot;branch_name&quot;: &quot;Russia Branch&quot;,
                  &quot;quantity&quot;: 0
              }
          ]
      },
      {
          &quot;id&quot;: 3,
          &quot;product_no&quot;: &quot;a000003&quot;,
          &quot;stock&quot;: [
              {
                  &quot;branch_name&quot;: &quot;US Branch&quot;,
                  &quot;quantity&quot;: 3.9
              },
              {
                  &quot;branch_name&quot;: &quot;India Branch&quot;,
                  &quot;quantity&quot;: 3.2
              },
              {
                  &quot;branch_name&quot;: &quot;Russia Branch&quot;,
                  &quot;quantity&quot;: 0
              }
          ]
      }
  ]
  </code></pre>
  <blockquote>
  <p>Error: Exception thrown: 'System.InvalidCastException' in MyProject.dll. <br/>
  An exception of type 'System.InvalidCastException' occurred in MyProject.dll but was not handled in user code.
  <br/> Unable to cast object of type 'WhereSelectEnumerableIterator<code>2[MyProject.Models.Product,System.Collections.Generic.List</code>1[MyProject.Models.ProductQuantity]]' to type 'System.Collections.Generic.List`1[MyProject.Models.ProductQuantity]'.</p>
  </blockquote>