19

I have the following method that load products on a DataGridView

private void LoadProducts(List<Product> products)
{
    Source.DataSource = products;  // Source is BindingSource
    ProductsDataGrid.DataSource = Source;
}

And now I'm trying to give me back to save them as shows below.

private void SaveAll()
{
   Repository repository = Repository.Instance;
   List<object> products = (List<object>)Source.DataSource; 
   Console.WriteLine("Este es el número {0}", products.Count);
   repository.SaveAll<Product>(products);
   notificacionLbl.Visible = false;
}

But I get an InvalidCastException on this line:

List<object> products = (List<object>)Source.DataSource;

So how can I cast the DataSource to an List?

daniele3004
  • 13,072
  • 12
  • 67
  • 75
Cristhian Boujon
  • 4,060
  • 13
  • 51
  • 90

4 Answers4

24

You can't cast covariantly directly to List;

Either:

List<Product> products = (List<Product>)Source.DataSource;

or:

List<Object> products = ((List<Product>)Source.DataSource).Cast<object>().ToList();
Dave Bish
  • 19,263
  • 7
  • 46
  • 63
  • For Line `List products = ((List)Source.DataSource).Cast();` Cannot implicitly type 'System.COllections.Generic.IEnumerable' to 'System.COllections.Generic.List. An explicit convertion exists (are you missing a cast?) – Cristhian Boujon Jan 11 '13 at 14:30
  • You need a .ToList() at the end, my bad! – Dave Bish Jan 11 '13 at 14:49
6

So how can I cast the DataSource to an List?

You have plenty of options

var products = (List<Product>)Source.DataSource; // products if of type List<Product>

or

 List<Object> products = ((IEnumerable)Source.DataSource).Cast<object>().ToList();

or

List<Object>  products = ((IEnumerable)Source.DataSource).OfType<object>().ToList();

or

List<Object> products = new List<Object>();
((IEnumerable)Source.DataSource).AsEnumerable().ToList().ForEach( x => products.Add( (object)x));
Tilak
  • 30,108
  • 19
  • 83
  • 131
  • `DataSource` doesn't implement `IEnumerable`, so you'll need to cast it to that first. – Servy Jan 11 '13 at 15:05
  • Same problem. `AsEnumerable` is an extension method of `IEnumerable`, not `object`. – Servy Jan 11 '13 at 15:09
  • Removed the last way. I was wondering How `Cast<>` can work and `AsEnumerable` cannot, while both accept `IEnumerable` as input? – Tilak Jan 11 '13 at 15:16
  • Neither can work, that's what I'm saying. You need to do `((IEnumerable)Source.DataSource).Cast()` or `OfType`. `DataSource` is of type `object` not `IEnumerable`. – Servy Jan 11 '13 at 16:29
3

Your List ist of type List<Product> which is different from List<object>. Try to cast to List<Product>

CubeSchrauber
  • 1,199
  • 8
  • 9
0

Convining the answers This is my solution:

private void SaveAll()
{
    Repository repository = Repository.Instance;
    List<Product> products = (List<Product>)Source.DataSource;
    IEnumerable<object> objects = products.Cast<object>();
    repository.SaveAll<Product>(objects.ToList<object>());
    notificacionLbl.Visible = false;
}

I accept constructive criticisms.

daniele3004
  • 13,072
  • 12
  • 67
  • 75
Cristhian Boujon
  • 4,060
  • 13
  • 51
  • 90