none
How can I convert from Anonymous type to DataRow of BindingSource current item which is bounded from IQueryable. RRS feed

  • Question


  • I have a bindingsource which is bounded by an IQueryable result of LINQ to SQL.

    private void loadProducts(short _engineID)
    {    
         dbLinqDataContext db
    = new dbLinqDataContext();                  
         
    var  productQuery = from pe in db.productEngines
                             where pe
    .EngineID == _engineID
                             
    select new
                             
    {
                               
    ProductID = pe.ProductID,
                               
    ProductNumber = pe.product.ProductNumber,
                               
    Description = pe.product.ProductName,
                               
    Cost = String.Format("{0:C}", pe.product.StandardCost),
                               
    Price = String.Format("{0:C}", pe.product.ListPrice),
                               QOH
    = String.Format("{0:0.00}",pe.product.QOH),
                               
    Picture = pe.product.Picture,
                               
    Category =pe.product.category.Category1,  

                               
    Engine=pe.engine.ModelSeriesYear,
                               
    Manufacturer=pe.engine.manufacturer.Manufacturer
                           
    };
    productQueryBindingSource.DataSource = productQuery;
    }

    The bindingSource works grate but  i am unable to convert my bindingsource current item to DataViewRow.

    Here is the error message:

    Unable to cast object of type '<>f__AnonymousType0`14[System.Int32,System.String,System.String,System.String,System.String,System.Nullable`1[System.Decimal],
    System.String,System.String,System.Byte[],System.String,System.Nullable`1[System.Int16],System.Int16,System.String,System.String]' to type 'System.Data.DataRowView'.

     

    DataRow currentRow = ((DataRowView)productQueryBindingSource.Current).Row;

    How can I cast my Anonymous Type to DataViewRow type?





    • Edited by sovantha Thursday, August 25, 2011 8:10 AM
    Thursday, August 18, 2011 2:57 AM

Answers

All replies

  • Hi sovantha;

    Can you post the code on how you do this, "i am unable to convert my bindingsource current item to DataViewRow."?

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, August 18, 2011 3:56 AM
  • I posted there.

    DataRow currentRow = ((DataRowView)productQueryBindingSource.Current).Row;

    I always use this snippet with bindingSource that hooks a dataTable in my Dataset. And it works fine. But i'm unable to covert with my bindingSource when it is bounded with a Anonymous type of LINQ to SQL.


    Thursday, August 18, 2011 5:05 AM
  • Hi sovantha;

    You state in your question the following, "The bindingSource works grate but i am unable to convert my bindingsource current item to DataViewRow.", this statement is true. A DataViewRow is a customized view of a DataRow which represents a row in a DataTable object of which the results of the Linq to SQL IQueryable<Anonymous> type is not, so therefore that is not possible without creating a DataRow object and filling it up.

    So in answer to you question, "How can I cast my Anonymous Type to DataViewRow type?", You can not.

    What is the end outcome you want to accomplish?

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, August 18, 2011 3:05 PM
  • IMO, we haven't direct way to convert anonymous type to others. But perhaps you can try to check this question, maybe help you. http://stackoverflow.com/questions/445984/convert-anonymous-type-to-class

    Thanks,

    Werewolf,


    Just a newbie for everything.
    • Marked as answer by Larcolais Gong Thursday, August 25, 2011 9:45 AM
    Friday, August 19, 2011 3:05 PM