Casting DataTable to typed when using DataTableExtensions.CopyToDataTable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Jocelyn

    Casting DataTable to typed when using DataTableExtensions.CopyToDataTable

    Hi

    I have a LINQ query

    var query = from t in TypedDataTable
    select t

    query contains Typed DataRows but I don't seem to be able to cast the
    returned DataTable from the DataTableExtens ions.CopyToData Table:

    DataSet.TypedTa ble tbl = (DataSet.TypedT able)query.Copy ToDataTable();

    What would be the best approach here?

    Thanks
    Andrew


  • Marc Gravell

    #2
    Re: Casting DataTable to typed when using DataTableExtens ions.CopyToData Table

    There is an overload:

    TypedTable table = new TypedTable();
    query.CopyToDat aTable(table, LoadOption.Pres erveChanges);

    your table should now contain the rows you want.

    I haven't tested it (I'm not really a DataTable person...) but give it
    a go ;-p

    You could perhaps write a custom extension method, but I'm not sure it
    would be worth it - since the table isn't an argument you end up
    losing type inference...

    Marc

    Comment

    • Andrew Jocelyn

      #3
      Re: Casting DataTable to typed when using DataTableExtens ions.CopyToData Table

      Hi Linda

      I was surprised that the CopyToDataTable method doesn't support Typed
      DataTables but the Merge method does what I need.

      Thanks
      Andrew


      Comment

      Working...