none
DataRow to Custom Object RRS feed

  • Question

  • Dear Friends,

         I have built a custom class with the Serializable and several XMLElement attributes as I need to serialize this class to XML as and end result.  I am using a typed DataSet with typed DataRows.  My issue is that I would like to automatically convert a DataRow or (a set of DataRows) to an instance (or instances) of my custom object.  I am not too sure about how to go about this.  I am NOT using the Entities Framework as I am not familiar with it yet.  Is there a way that LINQ can be used to automatically convert a DataRow to an instance of my custom object?  I would hope that if I use the SAME field names in the DataRow as my custom class, then maybe it would map the information automatically.  Any help would be greatly appreciated.

         Giovanni
    Tuesday, October 6, 2009 2:41 PM

Answers

  • Dim ds = new DataSet()
    FillOrders(ds, myConnectionString)

    Dim orders = ds.Tables("SalesOrderHeader").ToQueryable()
    Dim orderLines = ds.Tables("SalesOrderDetail").ToQueryable()

    Dim query = From o In orders _
                Where o!OnlineOrderFlag = True _
                Select SalesOrderID := o!SalesOrderID, _
                       OrderDate := o!OrderDate

    Narayanan Dayalan - Zeetaa Business Solutions Please Mark "Propose As Answer", if my answer works well with ur Query
    Monday, November 2, 2009 12:10 PM

All replies

  • Hi,

    Take a look at the ADO.NET Data Service. It does convert each of your datarow to your object and returns that over rest. Let me know if it helps.
    Monday, November 2, 2009 6:46 AM
  • Dim ds = new DataSet()
    FillOrders(ds, myConnectionString)

    Dim orders = ds.Tables("SalesOrderHeader").ToQueryable()
    Dim orderLines = ds.Tables("SalesOrderDetail").ToQueryable()

    Dim query = From o In orders _
                Where o!OnlineOrderFlag = True _
                Select SalesOrderID := o!SalesOrderID, _
                       OrderDate := o!OrderDate

    Narayanan Dayalan - Zeetaa Business Solutions Please Mark "Propose As Answer", if my answer works well with ur Query
    Monday, November 2, 2009 12:10 PM
  • Hi,

         Thank you very much for your reply.  I will look up some information on the Data Services libraries.
    Monday, November 2, 2009 7:46 PM
  • Hi,

         Thank you for your reply and code sample.  I will implement it in my project and view the results.  Once again, thank you.
    Monday, November 2, 2009 7:47 PM