I have a (disconnected) typed Dataset in an XML, which I query in LINQ:
var productlist = from prds in dsProducts.Products.AsEnumerable()
where (prds.Field<string>("productid").Contains(searchpattern) ||
prds.Field<string>("productname").Contains(searchpattern))
select prds;
This list is fine, but if I try to say:
return (DataSetProducts.productsDataTable)productlist.Skip(begin).Take(pagesize).CopyToDataTable();
It says it can not convert System.DataTable to DataSetProducts.productsDataTable, however it is the same table.
Any thoughts on how to return a typed DataTable?
Well,
CopyToDataTablehas no way of knowing what is the correct strongDataTabletype for a given type ofDataRow, since theDataRowdoesn’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 :
EDIT :
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
usingclause.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) :
Please note that I didn’t test this code, there might be a few bugs…