You are here > Blog

WebF1 Consulting Blog

Sep 13

Written by: Oscar Meszar
9/13/2010 6:41 PM 

Join queries in LINQ are pretty easy. This article might not be synthetically correct but you will get the idea.

Here is a simple query:

var Q = from c in Context.Contestants
       join e in Context.EventsParticipated on c.contestantID equals e.contestantID
       select c.Name + " participated in " + e.EventName;


Here is a complex key query:
var Q = from e in Context.Events
      join ed in Context.EventsDetail on new {e.Name, e.EventType} equals new {ed.Name, ed.EventType}
      select e.EventName + " - " + ed.EventYear;

Now let's take the first query and do something different...

var Q = from c in Context.Contestants
       join e in Context.EventsParticipated on e.contestantID equals c.contestantID
       select c.Name + " participated in " + e.EventName;

When you run the query above you will get an error similar to:
"The name 'e' is not in scope on the left side of 'equals'...."

So just make sure that you do your left join properly.

Happy LINQing.

Tags:

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
CAPTCHA image
Enter the code shown above in the box below
Add Comment   Cancel 
Search
Contact Us
Use our Contact Us page, or give us a call using the link below