Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Saturday, September 02, 2006

LinqToSql: Translating C# to SQL

How the LinqToSql libraries translates query expressions into T-SQL

This blog entry will finish the Where category of DLinq samples. Most of the logic is familiar, if you’ve read my earlier series on the core LINQ language enhancements.  However, there are some interesting twists on the way query language works when you are pulling data from a SQL database. But, you’ll only care if you are trying to round-trip the data, modifying it and storing it back in the database.

The first sample for today shows how First works with Linq to SQL:

public void DLinq6() {
    Shipper shipper = db.Shippers.First();
    ObjectDumper.Write(shipper, 0);

Just like we saw in the core LINQ libraries, First returns the first item in the collection. But, let’s walk through the code. The ‘db’ variable above is a member variable in the SampleQueries class:

private Northwind db;

The Northwind class is a specific class that maps objects from your object model to the Northwind (sample) database.  Here’s a small portion:

public partial class Northwind : DataContext {
    // snip
    public Table<Shipper> Shippers;
    // snip

Post a Comment