Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do select from two tables using Linq to SQL?

Posted on 2011-03-22
7
Medium Priority
?
723 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
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 count occurrences of each item in an array.

636 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