Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8705803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:27:22+00:00 2026-06-13T03:27:22+00:00

Im trying to convert the output of a LINQ query to a datatable, I

  • 0

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:27:24+00:00Added an answer on June 13, 2026 at 3:27 am

    I believe the problem is that you’re trying to convert an IEnumerable<T> of an arbitrary entity type – whereas CopyToDataTable() always requires the input to be a sequence of some kind of DataRow.

    Unless your entity type actually derives from DataRow, that’s not going to work. You could potentially write a LINQ query which creates a DataRow from each instance, but I believe you’ll have to write that code yourself.

    I believe in the question you referenced, the OP already had a strongly typed DataSet – at least the answer suggested that’s the case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to convert the following but am having difficulty. Can anyone see where I'm
I'm getting the following error when trying to get the sum in this Linq
I'm trying to convert my sample HTML output into a plain text but I
Trying to convert output from a rest_client GET to the characters that are represented
Trying to make manageable output for Android to convert $output into JSONObjects. When I
Trying to convert this c code into MIPS and run it in SPIM. int
I'm trying to persist an XML file to disk using LINQ. I have a
I'm trying to convert the output of a command like echo -e a b\nc\nd
I'm trying to convert an HTML block which is basically in the following form
I'm trying to figure out how to convert the following SQL statement to Entity

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.