• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 727
  • Last Modified:

How do select from two tables using Linq to SQL?

Two tables are related.
I have this code:

Dim dc As New DBDataContext

Dim q = From t In dc.taxes Join p In dc.persons On t.id Equals p.id _
Select t.rank, t.name, p.prop

      For Each t In q
                rowRank = t.rank
                rowName = t.name
                rowProp = p.prop
      Next
The p.prop does not work; p is not recognized as an object.

If I write
      For Each t In q
it works but the first two lines for t do not work.

How do I select data from mulitple tables?
                 
0
gs99
Asked:
gs99
  • 4
  • 3
1 Solution
 
DhaestCommented:
You can solve it very easily. Try this

        Dim q = From t In taxes.AsEnumerable() Join p In persons On t("id") Equals p("id") _
Select New With {
                .rank = t.rank, _
                .name = t.name, _
                .prop = p.prop _
            }


        For Each t In q
                rowRank = t.rank
                rowName = t.name
                rowProp = t.prop
        Next
0
 
gs99Author Commented:
Thanks,
Your suggestion works. However,
I had to change
Dim q = From t In taxes.AsEnumerable() Join p In persons On t("id") Equals p("id") _
to
Dim q = From t In taxes.AsEnumerable() Join p In persons On t.id Equals p.pid _

The results are that rows in taxes are selected ONLY if there is a row in persons with matching pid that equals the tax id column.

Perhaps I don't know how JOIN works.
I don't want persons data to affect selection of tax rows.
I only want to get persons data when it's available - if the pid matches.  
0
 
DhaestCommented:
I had to use the statement with t("id") because I just created 2 datatabels to test it.


>> The results are that rows in taxes are selected ONLY if there is a row in persons with matching pid that equals the tax id column.

Now you are using an inner join, which means that only records are returned that have in both tables the same "id".

What you want to achieve is a left (or right) join on a table, which means: take all the rows from this table and if found, take the row from the other table also
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
DhaestCommented:
Try the following example


        Dim q = From t In taxes _
                Group Join p In persons On t.id Equals p.id Into Group _
                From p In Group.DefaultIfEmpty()
        Select New With {
                        .rank = t.rank, _
                        .name = t.name, _
                        .prop = If(p Is Nothing, "", p.prop)) _
                    }
0
 
gs99Author Commented:
That works like I wanted.

Do you know of any books or websites that explain these things?
The syntax is so "not expected".

But my project can proceed again, thanks to you!
0
 
DhaestCommented:
I don't know any books, but a good starting point is always:

How to: 101 LINQ Samples for Visual Basic.NET
http://rshelton.com/archive/2008/02/03/how-to-101-visual-basic-linq-samples.aspx
0
 
gs99Author Commented:
Thanks
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now