Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / SQL
Article

Dissecting LINQ to SQL

Rate me:
Please Sign up or sign in to vote.
4.67/5 (24 votes)
28 Feb 2008CPOL9 min read 104.5K   70   9
Where LINQ to SQL shines as compared to other ORM tools in the market is the fact that it abstracts away the database structure behind our business objects, it also supports a query language called LINQ.

Introduction

The following example requires generating a mapping file for a Northwind database.

LINQ to SQL solves the mismatch between the object oriented and relational database world. In an application we deal in terms of our domain objects such as customer and orders. However, when it comes to saving these objects and solving the datatype mismatch between the programming and database world, it has traditionally been the developer's responsibility to write code to persist these objects to a database and handle the data type conversions. Where LINQ to SQL shines as compared to other ORM tools in the market is the fact that not only LINQ to SQL abstracts away the database structure behind our business objects, it also supports a query language called LINQ. Although LINQ to SQL provides a query language, you can still write your queries using stored procedure or dynamic SQL. LINQ to SQL maps your entity classes to database tables and entity class properties to database table columns. Let’s see an example.

C#
/// <summary>
/// Examples shows how to dynamically insert rows into Northwind database without

/// calling sumbit changes.
/// </summary>
public static void DynamicInsert()
{
    NorthWindDataContext db = new NorthWindDataContext();
    db.ExecuteCommand("insert into shippers(companyname,
        phone) values({0},{1})", "Test shipper", "(111)111-1111");
    var shipper = db.Shippers.Single(s =>

    s.CompanyName == "Test shipper");
    Console.WriteLine(shipper.ShipperID);
}

The example above shows how we can directly insert record into Northwind without using our shipper object and then querying the database using our context to confirm that record got inserted. Not only can we insert but we also have the ability to query database using dynamic SQL and directly get our objects back. The benefit of going that route would be to write your own SQL but not have to deal with issues of converting datareader to our objects because Datacontext takes care of it as long as the fields returned from SQL matches the properties on your objects. Let see an example of that.

C#
public static void DynamicSql()
{
    NorthWindDataContext db = new NorthWindDataContext();
    IEnumerable<Category>

    categories = db.ExecuteQuery<Category>(
       "select categoryid,categoryName CatName from categories where categoryname = {0}",
       "Beverages");
    foreach (var cat in categories)
    {
        Console.WriteLine(cat.CategoryName);
    }
}

When you run the above query you will notice that category name would be set to null. When you are constructing a dynamic SQL, the column name needs to match with the properties on your object(Category). Also notice that we are not returning all the fields from our database and yet we do not get any exception. The reason for that is that LINQ to SQL will not set any values on the properties that are not returned from the SQL. However, when your dynamic SQL statement does not contain the column which is defined as primary key, LINQ to SQL will raise a runtime exception as shown below.

C#
/// <summary>
/// this example demonstrates that if the dynamic sql statment does not have primary
/// key linq to sql will raise exception
/// </summary>

public static void DynamicSqlWithNoPrimaryKey()
{
    NorthWindDataContext db = new NorthWindDataContext();
    IEnumerable<Category> categories = db.ExecuteQuery<Category>(
        "select categoryName from categories where categoryname = {0}", "Beverages");
    foreach (var cat in categories)
    {
        Console.WriteLine(cat.CategoryName);
    }
}

Another useful method that I found on the datacontext is translate. The translate method basically takes in a datareader and returns a collection of objects. This would come in handy on existing data access layers which returns a datareader and you can use the translate method to get your business objects back. Let's see an example.

C#
/// <summary>
/// This example illustrates how you can use datareaders to get strongly typed objects.
/// </summary>

public static void ReturnObjectsUsingDataReader()
{
    NorthWindDataContext db = new NorthWindDataContext();
    db.Connection.Open();
    SqlCommand cmd = new SqlCommand("select categoryid,categoryName,
        Description from categories",
        db.Connection as SqlConnection);
    IEnumerable<Category> categories = db.Translate<Category>(
        cmd.ExecuteReader(CommandBehavior.CloseConnection));
    foreach (var category in categories)
    {
        Console.WriteLine(category.CategoryName);
    }
}

There are times when you would be interested in knowing what LINQ to SQL does under the covers. There are various methods to capture the SQL. For example, you can use SQL Server profile. Datacontext has a property called log which can be used to output the SQL or you can use the getquerytext method on datacontext to find out what SQL would execute if you were to execute the LINQ to SQL query. Let's have a look at an example.

Overriding Insert,Update and Delete Methods

LINQ to SQL allows you to use stored procedures and specify your own implementation for inserting an object into the database. Going that route would require more involvement from the developer side which includes resolving change conflicts that could occur. One way to accomplish the task is using the designer by dragging the stored procedure onto the method pane. And right-clicking on Category to configure its behavior as follows.

SQL
create proc dbo.InsertCategory
(
 @CategoryID int output,
 @CategoryName nvarchar(15),
 @Description nvarchar(100)
)

as
begin

insert into dbo.categories(categoryname,[description])
values (@CategoryName,@Description)
set @CategoryID = @@identity

end

image001.gif

Once you are done configuring the wizard with the stored procedure you can insert category as follows.

C#
/// <summary>
/// Example illustrates how to insert category using stored procedures.
/// </summary>

public static void InsertCategoryUsingStoredProcedure()
{
    NorthWindDataContext db = new NorthWindDataContext();
    Category category = new Category { CategoryName = "test",
        Description = "test description" };
    db.Categories.InsertOnSubmit(category);
    db.SubmitChanges();
    Console.WriteLine(category.CategoryID);
}

image002.gif

Inserting Object Graph

When you are inserting an object by calling insertonsubmit, LINQ to SQL does not actually insert the row until you call submit changes. One of the nice things about LINQ to SQL is if you have an object hierarchy, say: Category, Product and supplier, which are related to each other in the hierarchy you don’t necessarily have to call insertonsubmit on each object. Calling on the base object like category is sufficient enough for LINQ to SQL to save all the objects including product and supplier. Let's look at an example of this

C#
/// <summary>

/// This example illustrates that you do not need to call insertonsubmit 
/// for each object you like to persist to database. calling it on the base object is
/// sufficent.
/// </summary>

public static void InsertingHiearachy()
{
    NorthWindDataContext db = new NorthWindDataContext();
    Category category = new Category
    {
        CategoryName = "Test",
        Description = "test description",
        Products =
        {
            new Product
            {
                ProductName="my product",
                Supplier= new Supplier{CompanyName="test company"}
            }
        }
};
db.Categories.InsertOnSubmit(category);
db.SubmitChanges();
var cat = db.Categories.Single(c =>

c.CategoryID == category.CategoryID);
Console.WriteLine(cat.CategoryName);
Console.WriteLine(cat.Products[0].ProductName);
Console.WriteLine(cat.Products[0].Supplier.CompanyName);
}

image003.gif

You will notice from the above example that we did not call insertonsubmit on product or supplier but instead LINQ to SQL because the relationship was able to insert all three wobjects when submit changes were called.

Since all the objects returned from datacontext are returned as Table<T> which happens to implement IQueryable<T>, therefore most LINQ to SQL queries are differed and converted into expression trees instead of intermediate language code. Once they are converted to expression trees, the LINQ to SQL provider converts the expression tree into SQL and executed on the database. Because the queries are executed on the database, calling a .NET method inside of a query does not have any translation to the database, which would cause the query to fail.

LINQ TO SQL Differed Exection

By default LINQ to SQL will not load up any child entity, unless the child property is called. This behavior is called differed exection. This behavior ensures that LINQ to SQL to not retrieve any child objects unless they are explicitly referenced. Let's look at an example.

SQL
public static void LinqDifferedExection()
{
    NorthWindDataContext db = new NorthWindDataContext();
    db.Log = Console.Out;
    var customer = db.Customers.First();
    Console.WriteLine(customer.ContactName);
    Console.WriteLine(customer.Orders.Count());
}

image004.gif

If you look at the SQL statements executed you will notice that there were two separate queries executed. And it is only when the Orders object was accessed that LINQ to SQL made an additional call to access all the orders for that customer.

Although differed loading is the default behavior of LINQ to SQL, in a case where you do not want to incur the hit everytime you access a particular child object you can use dataloadoptions to tell LINQ to SQL to immediately load the desired class specified in the loadWith<T> operator. When using LoadWith<T> the associated class is retrieved when LINQ to SQL queries for the primary class. Immediate loading is not limited to loading one associated class. You can provide as many associated classes that you would need to be loaded ahead of being called. Let's look at an example.

C#
public static void ImmedidateLoading()
{
    NorthWindDataContext db = new NorthWindDataContext();
    db.Log = Console.Out;
    DataLoadOptions option = new DataLoadOptions();
    option.LoadWith<Customer>(c => c.Orders);
    db.LoadOptions = option;
    var customers = db.Customers.Where(c => c.City == "London");
    foreach (var customer in customers)
    {
        Console.WriteLine(customer.ContactName);
        foreach (Order order in customer.Orders)
        {
            Console.WriteLine(order.OrderID);
        }
    }
}

image005.gif

Looking at an above example you would notice that we are looping through the order collection of customer, but only 1 single query was executed by LINQ to SQL because we are using loadoptions which ensures that all the orders for the related customers are loaded ahead of time.

The behavior of immediate loading changes when you are loading multiple associated classes. Only the first one will be joined with the primary class and others will be loaded when you reference the primary class. However, it is still considered immediate because the LINQ to SQL will query the object before you actually reference the object. A separate query would be performed to pull the data for associated class every time you access the primary class. For this demo I have added another table to Northwind called Address. Basically I will immediately load all addresses and orders for a particular customer.

SQL
CREATE TABLE [dbo].[Address](

[AddressId]
[int] IDENTITY(1,1) NOT NULL,

[Address1]
[varchar](50) NULL,

[CustomerID]
[nchar](5) NOT NULL,

)

public static void ImmediateMultipleAssociations()
        {
            NorthWindDataContext db = new NorthWindDataContext();
            db.Log = Console.Out;
          
 
            DataLoadOptions option = new DataLoadOptions();
            option.LoadWith<Customer>(c => c.Addresses);
            option.LoadWith<Customer>(c => c.Orders);
            db.LoadOptions = option;
            var customers = db.Customers.Where(c => c.City == "London");
            foreach (Customer customer in customers)
            {
                Console.WriteLine(customer.ContactName);
            }
        }

image006small.GIF

When looking at query generated by LINQ to SQL, you will notice that customers were joined to the orders table but a seprate query was run for each customer to access its address every time a customer object was referenced. Since neither order nor order details are being referenced in the code, we can correctly say that it is performing immediate loading. However, the address query is being executed for every customer.

Let's look at an example where we load customer, its orders and for each order its order detail using immediate loading.

C#
public static void ImmmediateLoadingWithDependency()
        {
            NorthWindDataContext db = new NorthWindDataContext();
            db.Log = Console.Out;
            DataLoadOptions option = new DataLoadOptions();
            option.LoadWith<Customer>(c => c.Orders);
            option.LoadWith<Order>(o => o.Order_Details);
            db.LoadOptions = option;
            var customers = db.Customers.Where(c => c.City == "London");
            foreach (Customer customer in customers)
            {
                Console.WriteLine(customer.ContactName);
            }

image007small.GIF

Based on the query you will notice the same behavior where a separate query was performed to access the order table every time Customer was being referenced inside the foreach loop. The only main difference between this example and the previous example is this time LINQ to SQL only retrieved Customers. It was only when the customer object was being referenced inside the for loop that LINQ to SQL not only queried order but also the order details table.

Another use of data load options is to filter the associated classes by using associatedWith method. As you can see in the result below that although each categories have more than 1 product, we use LINQ to SQL to retrieve only 1 product for each category because of the filter applied on AssociateWith method on DataLoadOptions.

C#
public static void FilteringAssociatedClasses()
        {
            NorthWindDataContext db = new NorthWindDataContext();
           
            DataLoadOptions option = new DataLoadOptions();
            option.AssociateWith<Category>(c => c.Products.Take(1));
            db.LoadOptions = option;                                   
            foreach (Category category in db.Categories)
            {
                Console.WriteLine(category.CategoryName);
                Console.WriteLine(category.Products.Count());
 
            }
        }

image008.gif

Operator Conversion

There are numerous methods that are defined in .NET to manipulate strings. Some of them have equivalent translation in LINQ to SQL and gets executed on SQL Server.

C#
public static void OperatorConversions()
        {
            NorthWindDataContext db = new NorthWindDataContext();
            db.Log = Console.Out;
            string[] cities =  {"London","Seattle"};
            var orders = db.Orders.Where(o => cities.Contains(o.ShipCity));
            Console.WriteLine(orders.Count());
        }

image009.gif

Notice how we are using the "contains" clause which gets converted to the "in" clause in SQL Server.

DataContext Query Behavior

As I mentioned in the start of the article that LINQ to SQL will not execute you query unless submit changes is called. So if you add a customer to the customer collection and then query the datacontext you will not get that record back since submit changes has not been called. Let's take a look at an example of that.

C#
public static void SubmitChangesBehavior()
        {
            NorthWindDataContext db = new NorthWindDataContext();
            db.Log = Console.Out;
            Customer customer = new Customer { CustomerID = "TestID",
                CompanyName = "Test Company" };
            db.Customers.InsertOnSubmit(customer);
 
            var _customer = db.Customers.SingleOrDefault(c => c.CustomerID == "TestID");
            Console.WriteLine(
                _customer == null ? "Customer not found" : "Customer found");
        }

image010small.GIF

There are two things to notice. First, despite the fact that LINQ to SQL was tracking the object in its context; it made a query to SQL Server to search for the customer with a specific customerid. Secondly, you will also notice that no customer was found.

This confirms the fact that customer object will not be returned from the context unless submit changes is called.

Another interesting behavior which I have noticed with the single operator is when you query for an object based on its primary key, the next time you query with same primary key, LINQ to SQL does not even go to the database, it retrieves the object from its internal cache. Let's have a look at an example of that.

C#
public static void LinqToSqlCachingBehavior()
        {
            NorthWindDataContext db = new NorthWindDataContext();
            db.Log = Console.Out;
            var cust1 = db.Customers.Single(c => c.CustomerID == "ALFKI");
            Console.WriteLine(cust1.ContactName);
            var cust2 = db.Customers.Single(c => c.CustomerID == "ALFKI");
            Console.WriteLine(cust2.ContactName);
        }

image011small.GIF

As you will notice from the result that the query got retrieved only once. Even though we requested it twice because we were querying based on primary key. If this would not have been the case then LINQ to SQL would return the same object but would still execute the query both the times. Let's have a look at an example of that.

C#
public static void LinqToSqlSameObjectReturned()
        {
            NorthWindDataContext db = new NorthWindDataContext();
            db.Log = Console.Out;
            var cust1 = db.Customers.Single(c => c.ContactName == "Maria Anders");
            Console.WriteLine(cust1.ContactName);
            var cust2 = db.Customers.Single(c => c.ContactName == "Maria Anders");
            Console.WriteLine(cust2.ContactName);
            Console.WriteLine(cust1 == cust2);
        }

image012small.GIF

You will notice from the result that although the reference to the object was the same, LINQ to SQL executed the query twice.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Tarrant Regional Water District
United States United States
General Certificate of Education obtained in Math, Physics, Chemistry, Principle of Accounts & Statistics in the year 1998-99 from the University of Cambridge, London, England.
Earned a degree in Business Administration in Information System with a GPA of 3.6 on 4.0 scales from Univ. of Texas At Arlington in Honors in the year 2005.
Certified MCAD,MCSD,MCPD ,OCP (Oracle Certified Professional),MCDBA.

I have been working as an Asp.net C# developer for last 3 years.

Zeeshan Hirani
Senior Asp.net Developer
Tarrant Regional Water District
zhirani@trwd.com
http://www.trwd.com



Comments and Discussions

 
GeneralMy vote of 5 Pin
AntonioLopes14-Feb-12 14:41
AntonioLopes14-Feb-12 14:41 
GeneralSome corrections Pin
Phil J Pearson17-Jan-11 5:37
Phil J Pearson17-Jan-11 5:37 
Generalgood one Pin
Pranay Rana15-Dec-10 19:51
professionalPranay Rana15-Dec-10 19:51 
Generalgood job Pin
shahed hasan24-Oct-08 23:35
shahed hasan24-Oct-08 23:35 
GeneralNice and Helpful Pin
Jahedur Rahman Chowdhury14-Aug-08 23:55
Jahedur Rahman Chowdhury14-Aug-08 23:55 
QuestionCache does not work ? Pin
Philippe FLEUR20-Apr-08 2:43
Philippe FLEUR20-Apr-08 2:43 
GeneralGood Article Pin
merlin9815-Mar-08 3:55
professionalmerlin9815-Mar-08 3:55 
GeneralRe: Good Article Pin
lunky2-Sep-08 6:21
lunky2-Sep-08 6:21 
GeneralVery good article Pin
oliwan21-Feb-08 9:05
oliwan21-Feb-08 9:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.