Nested Selects in LINQ to SQL

Consider the following diagram to track the votes in a contest. Each voter can register only one vote (a score from 1 to 4) for each contest entry.

For any given voter, you'll want to present a screen showing every entry in the competition, along with the score assigned to the entry by the voter (if a score exists). To fetch the data with SQL, I'd write something like:

SELECT E.EntryID, E.EntryName, E.EntryDescription, @VoterID AS VoterID,
       (SELECT V.Score
        FROM   Votes AS V
        WHERE (V.VoterID = @VoterID) AND (V.EntryID = E.EntryID)) AS Score
FROM Entries AS E

The query should yield a resultset like the following:

...
EntryID EntryName VoterID Score 13 EntryA 1 4 14 EntryB 1 2 15 EntryC 1 NULL ...

I thought the query would be difficult to express in LINQ, but I stumbled into a solution just by keeping a "SQL" mindset:

var q = from e in Entries
         
select new
         
{
               EntryID = e.EntryID,
               EntryName = e.EntryName,
               EntryDescription = e.EntryDescription,
               VoterID = voterID,
               Score = (
int?)(from v in e.Votes
                             
where v.VoterID == voterID
                             
select v.Score).FirstOrDefault()
};

The LINQ generated SQL looks amazingly similar to the hand generated SQL, which is a comforting sight!

Print | posted @ Monday, September 24, 2007 4:22 AM

Comments on this entry:

Gravatar # re: Nested Selects in LINQ to SQL
by Rick Strahl at 9/25/2007 8:19 AM

I'm really digging the way you can work with LINQ queries and the above is something I was just doing last night as well.

I think the part that I like more than anything is that I can create somewhat generic business logic for my queries in a business layer, pass out the query and then further manipulate the data and winnie it down to EXACTLY the data that I need for stuff like databinding or assignments. The end result is that I can create precise SQL that returns only what I need without having to have many overloads of business object methods.

Takes some time getting used to the syntax though and maybe more importantly trusting LINQ to SQL to do the right thing.
  
Gravatar # re: Nested Selects in LINQ to SQL
by Noah Coad at 10/18/2007 6:40 PM

Nice! I use plenty of embedded SQL statements, so it's nice to see LINQ supports it so well. The .FirstOrDefault() method looks quite useful. (btw, your images are not coming through in the RSS feed)
  

Your comment:

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 4 and 4 and type the answer here: