How to Casting DataSource to List?

后端 未结 4 1533
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-17 11:11

I have the following method that load products on a DataGridView

private void LoadProducts(List products)
{
    Source.DataSource = products;          


        
4条回答
  •  情歌与酒
    2021-01-17 11:40

    So how can I cast the DataSource to an List?

    You have plenty of options

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

    or

     List products = ((IEnumerable)Source.DataSource).Cast().ToList();
    
    
    

    or

    List  products = ((IEnumerable)Source.DataSource).OfType().ToList();
    
    
    

    or

    List products = new List();
    ((IEnumerable)Source.DataSource).AsEnumerable().ToList().ForEach( x => products.Add( (object)x));
    
        

    提交回复
    热议问题