?
Solved

How do select from two tables using Linq to SQL?

Posted on 2011-03-22
7
Medium Priority
?
717 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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.

 
LVL 53

Accepted Solution

by:
Dhaest earned 500 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

777 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