score:2

Accepted answer

well, copytodatatable has no way of knowing what is the correct strong datatable type for a given type of datarow, since the datarow doesn't provide that information (which is a shame imho).

perhaps you could write your own copytodatatable method, which would take another type parameter to specify the table type. something like that :

public static ttable copytodatatable<trow, ttable>(this ienumerable<trow> rows)
  where trow : datarow, new()
  where ttable : datatable, new()
{
    ttable table = new ttable();
    foreach (trow row in rows)
    {
        trow rowcopy = new trow();
        object[] itemarraycopy = new object[row.itemarray.length];
        row.itemarray.copyto(itemarraycopy, 0);
        rowcopy.itemarray = itemarraycopy;
        table.rows.add(rowcopy);
    }
    return table;
}

edit :

how can i use this new function in the above example?

you must put this extension method in a static class. if you put the class in a different namespace, make sure to import that namespace in the scope with a using clause.

you can then use the method like that (you must specify the type parameters since the ttable type can't be inferred by the compiler) :

return productlist.skip(begin).take(pagesize).copytodatatable<datasetproducts.productsrow, datasetproducts.productsdatatable>();

please note that i didn't test this code, there might be a few bugs...


Related Query

More Query from same tag