Solved

How do select from two tables using Linq to SQL?

Posted on 2011-03-22
7
709 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now