I have the following datatables
table1:
+-----------+------+------+
| catalogid | name | snum |
+-----------+------+------+
| 353 | xx | 4 |
| 364 | yy | 3 |
| 882 | zz | 3 |
| 224 | mm | 71 |
| 999 | kk | 321 |
| 74 | kk | 4 |
| 54 | ii | 5 |
| 11 | u | 6 |
| 23 | yy | 6 |
+-----------+------+------+
table2:
+-----------+----------+--------------+
| catalogid | numitems | ignoreditems |
+-----------+----------+--------------+
| 353 | 4 | 0 |
| 364 | 10 | 0 |
| 882 | 2 | 0 |
| 224 | 0 | 7 |
+-----------+----------+--------------+
Using LINQ I want to join them and copy the result to a new datatable. They both share catalogid, and in the result it should only display the records that their catalogid exist in table2
result:
+-----------+------+------+-----------+---------------+
| catalogid | name | snum | numitems | ignoreditems |
+-----------+------+------+-----------+---------------+
| 353 | xx | 4 | 4 | 0 |
| 364 | yy | 3 | 10 | 0 |
| 882 | zz | 3 | 2 | 0 |
| 224 | mm | 71 | 0 | 7 |
+-----------+------+------+-----------+---------------+
Here is my attempt but it’s not working:
Dim query = From a In oresult.AsEnumerable
Group Join b In products.AsEnumerable
On a.Field(Of Integer)("catalogid") Equals b.Field(Of Integer)("catalogid")
Into Group
query.copytodatatable
CopyToDatatable is not working, and I can’t figure out why
CopyToDataTable() only works when your query returns an IEnumerable<‘DataRow>. In your query, you are returning an anonymous type. Anonymous types don’t carry the extension method for CopyToDataTable().
You can create a table using the ConvertToDataTable extension listed below. You’ll have to convert it to VB.NET (there are converters out there if you google).