Solved

How do select from two tables using Linq to SQL?

Posted on 2011-03-22
7
711 Views
Last Modified: 2012-05-11
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
Comment
Question by:gs99
  • 4
  • 3
7 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 35196439
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
 

Author Comment

by:gs99
ID: 35197500
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
 
LVL 53

Expert Comment

by:Dhaest
ID: 35197582
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 53

Accepted Solution

by:
Dhaest earned 125 total points
ID: 35197633
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
 

Author Closing Comment

by:gs99
ID: 35198280
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
 
LVL 53

Expert Comment

by:Dhaest
ID: 35198294
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
 

Author Comment

by:gs99
ID: 35198691
Thanks
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to dynamically set the form action using jQuery.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question