2

I am building a table based upon a set of queries that will determine what the columns of the table are. Which means that the column names will be dynamic. Each of these queries will have an id which we are using to match the correct data to the correct user.

Here is what the data looks like:

Base Table

base table

Sub Table

sub table

The code I am using is

Dim tablesJoined = From t1 In dt1.AsEnumerable()
                   Join t2 In flatTable.AsEnumerable()
                   On t1.Field(Of String)("SHM_ID") 
                   Equals t2.Field(Of String)("SHM_ID")

However, I want to select all of the values for dt1 and merge the data from the second table into it. For further context, I will be doing this several times, so I can't just use a static object.

Is there a way to put these two tables together in an elegant manner?

It would look something like this:

Combined Table

djv
  • 15,168
  • 7
  • 48
  • 72
  • Perhaps you can use Select New With... (https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/linq/how-to-return-a-linq-query-result-as-a-specific-type) – NoAlias Nov 20 '17 at 21:26
  • What would the output look like? Can you build the desired table structure and put it in your question? – djv Nov 20 '17 at 21:26
  • `Datatable.Merge`? – rheitzman Nov 20 '17 at 21:50
  • @ N0Alias Select New With would work, if I knew all of the columns ahead of time. However, unless I am mistaken, you are building out an object with that method where you need to know the parameters for the object. Is there a way to build the object in the query where it is based upon the rows present within the first datatable? – William Tsikerdanos Nov 20 '17 at 21:53
  • @djv Added to the question. – William Tsikerdanos Nov 20 '17 at 22:41
  • @rheitzman Merge seems to be just appending the other tables onto the end of the first table rather than matching them based upon that field. – William Tsikerdanos Nov 20 '17 at 22:47
  • 1
    I am not clear on your requirements, but you may find some ideas in this old non-LINQ MS support article: [HOW TO: Implement a DataSet JOIN Helper Class in Visual Basic .NET](https://support.microsoft.com/en-us/help/325688/how-to-implement-a-dataset-join-helper-class-in-visual-basic--net) – TnTinMn Nov 21 '17 at 01:58
  • I can be a little late but is this a dupe : https://stackoverflow.com/questions/389094/how-to-create-a-dynamic-linq-join-extension-method – Drag and Drop Nov 21 '17 at 10:08

0 Answers0