Im trying to convert the output of a LINQ query to a datatable, I have the following code but it shows a syntax error in the (Of DataRow)
part:
Dim X As New Entities Dim query As IEnumerable(Of DataRow) = From cajero In X.CAJERO.AsEnumerable Select cajero Dim bla = query.CopyToDataTable(Of DataRow)()
I'm using this question as a guide: Filling a DataSet or DataTable from a LINQ query result set
If i use
query.CopyToDataTable() 'instead of the overload query.CopyToDataTable(Of DataRow)
it throws an invalidCastException.
I'm looking for an easy way to accomplish this task, and this seemed to be the easiest one, without too much code and having to implement any "shredder" methods or such, but if that's the only way, then please point me in the right direction.
This is the error that throws(I localized it to english, its a bit different in spanish):
Cannot convert object of type WhereSelectEnumerableIterator to object of type IEnumerable System.Data.DataRow
I have tried the following:
Declare an extension method that would create the datatable like this:
_ Public Function myToDataTable(Of T)(source As IEnumerable(Of T)) As DataTable Dim properties As PropertyInfo() = GetType(T).GetProperties() Dim output As New DataTable() For Each prop In properties output.Columns.Add(prop.Name, prop.PropertyType) Next For Each item In source Dim row As DataRow = output.NewRow() For Each prop In properties row(prop.Name) = prop.GetValue(item, Nothing) Next output.Rows.Add(row) Next Return output End Function
But it always throws an exception while adding the columns to the datatable:
DataSet does not support System.Nullable
I also changed the linq query to this:
Dim query = From cajero In X.CAJERO Select cajero Dim bla = query.myToDataTable
Following Jon's suggestion I found this question:
.NET - Convert Generic Collection to DataTable
Which just gave me the last bit of code I needed:
output.Columns.Add(prop.Name, If(Nullable.GetUnderlyingType(prop.PropertyType), prop.PropertyType))
and
row(prop.Name) = If(prop.GetValue(item, Nothing), DBNull.Value)