none
How to use CopyToDatatable to an existing TYPED datatable RRS feed

  • Question

  • Hi guys!

    I cant copy the results of a LINQ query to a Dataset using the CopyToDatatable extension, it works fine for untyped datatables:

    Dim q_resasesor = (linq )

    Dim qtable As DataTable = q_resasesor.CopyToDataTable()

     

    How can i use it to copy data to an existing TYPED datatable?    i tried the next code but get an error … “existing column”

    Dim dtpreind As DSRHPROD.PREINDICDataTable

    dtpreind = dsrhprod.PREINDIC

    q_resasesor.CopyToDataTable(dtpreind, LoadOption.PreserveChanges)

     

     Is there another way to copy data from untyped datatable1  to an my TYPED ds.datatable2

    Untyped Datatable1 has five fields:           [ f1, f2, f3, f4, f5 ]

    Typed Ds.datatable2 has twelve fields:     [ f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12 ]

     If I use merge, get an error about null fields!!!

     Thanks in advance!!!

     

    Wednesday, July 6, 2011 5:28 PM

Answers

  • H arzamm;

    To your statement, "I cant copy the results of a LINQ query to a Dataset using the CopyToDatatable extension, it works fine for untyped datatables:", The DataTableExtensions.CopyToDataTable(Of T) Method (IEnumerable(Of T)) only copies DataRow objects from one DataTable to another and the results of a Linq to SQL query can't be used to do this.

    From the documentation, "The parameter T of the input parameter source can only be of type DataRow or a type derived from DataRow.", CopyToDataTable.

    Therefore the only reason possibly for this to be working for you is that one the Linq query is a Linq to DataSet returning a result set of IEnumerable of DataRow or two the CopyToDatatable you are using is one developed in your company.

    As for the second question, "How can i use it to copy data to an existing TYPED datatable?", The type of Table of a typed DataTable is NOT of the same type of the untyped DataTable. So in order to copy the rows of a Typed DataTable to an Untyped DataTable will need to be done manually, there is no API that I know of to do this.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, July 6, 2011 6:22 PM