Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Copytodatatable usage in VB

Copytodatatable usage in VB

Scheduled Pinned Locked Moved LINQ
csharpdatabasedotnetlinqregex
3 Posts 3 Posters 5 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ahawth
    wrote on last edited by
    #1

    Using VB targeting V4.0 of the .net framework, I'm trying to filter an in-memory datatable using linq and want the results in a datatable. For example, the following linq query works:

    dim query=(from customers in dtcustomers.asenumerable
    where customers.state = "VA"
    select customers).copytodatatable

    This works as long as the query returns at least 1 match from the table. If no match is found, I end up with an unhandled InvalidOperationException because the source contains no datarows. Every example I can find of copytodatatable usage is in this form:

    dim query=from customers in dtcustomers.asenumerable
    where customers.state = "VA"
    select customers

    dim dtNewTable as datatable=query.copytodatatable

    My thought was to use something similar to the second example and test for a result from the query prior to calling copytodatatable, but anytime I try to use copytodatatable outside the query itself, I get : "Public member 'copytodatatable' on type 'EnumerableRowCollection(Of CustomerRow)' not found." I've seen plenty of similar posts about MissingMemberExceptions when using copytodatatable but it's usually due to a missing reference. I've added the reference and 'copytodatatable' works but only if it's in the query itself. Any thoughts why? Thanks in advance.

    M P 2 Replies Last reply
    0
    • A ahawth

      Using VB targeting V4.0 of the .net framework, I'm trying to filter an in-memory datatable using linq and want the results in a datatable. For example, the following linq query works:

      dim query=(from customers in dtcustomers.asenumerable
      where customers.state = "VA"
      select customers).copytodatatable

      This works as long as the query returns at least 1 match from the table. If no match is found, I end up with an unhandled InvalidOperationException because the source contains no datarows. Every example I can find of copytodatatable usage is in this form:

      dim query=from customers in dtcustomers.asenumerable
      where customers.state = "VA"
      select customers

      dim dtNewTable as datatable=query.copytodatatable

      My thought was to use something similar to the second example and test for a result from the query prior to calling copytodatatable, but anytime I try to use copytodatatable outside the query itself, I get : "Public member 'copytodatatable' on type 'EnumerableRowCollection(Of CustomerRow)' not found." I've seen plenty of similar posts about MissingMemberExceptions when using copytodatatable but it's usually due to a missing reference. I've added the reference and 'copytodatatable' works but only if it's in the query itself. Any thoughts why? Thanks in advance.

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      I don't use this stuff but it occurs to me that you could do a count() query first, test the value and then do the datatable query!

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • A ahawth

        Using VB targeting V4.0 of the .net framework, I'm trying to filter an in-memory datatable using linq and want the results in a datatable. For example, the following linq query works:

        dim query=(from customers in dtcustomers.asenumerable
        where customers.state = "VA"
        select customers).copytodatatable

        This works as long as the query returns at least 1 match from the table. If no match is found, I end up with an unhandled InvalidOperationException because the source contains no datarows. Every example I can find of copytodatatable usage is in this form:

        dim query=from customers in dtcustomers.asenumerable
        where customers.state = "VA"
        select customers

        dim dtNewTable as datatable=query.copytodatatable

        My thought was to use something similar to the second example and test for a result from the query prior to calling copytodatatable, but anytime I try to use copytodatatable outside the query itself, I get : "Public member 'copytodatatable' on type 'EnumerableRowCollection(Of CustomerRow)' not found." I've seen plenty of similar posts about MissingMemberExceptions when using copytodatatable but it's usually due to a missing reference. I've added the reference and 'copytodatatable' works but only if it's in the query itself. Any thoughts why? Thanks in advance.

        P Offline
        P Offline
        prasun r
        wrote on last edited by
        #3

        Have you tried getting a list of all the rows and then do a CopyToDataTable

        dim query=(from customers in dtcustomers.asenumerable
        where customers.state = "VA"
        select customers).ToList

        dim dtNewTable as datatable=query.copytodatatable

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups