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:
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:
The LINQ generated SQL looks amazingly similar to the hand generated SQL, which is a comforting sight!
Comments
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.
FirstOrDefault() will return "int" and not "int?" always. default(int) will return 0. Ignore this comment if you already considered this.