Copytodatatable usage in VB
-
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).copytodatatableThis 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 customersdim 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.
-
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).copytodatatableThis 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 customersdim 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.
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
-
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).copytodatatableThis 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 customersdim 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.