Home > Articles > Programming > ASP .NET

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Executing LINQ to SQL Queries

After you create one or more LINQ to SQL entities by using the Object Relational Designer, you can start performing LINQ to SQL queries. You can write a LINQ to SQL query using either method syntax or query syntax.

Let’s start with method syntax. Suppose that you want to retrieve a set of entities representing all of the rows from the Products database table. You can use the code in Listing 1.

Listing 1 Product.cs (method syntax).

using System;
using System.Linq;
using System.Data.Linq;
using System.Collections.Generic;

public partial class Product
{
  public IEnumerable<Product> Select()
  {
    StoreDataContext db = new StoreDataContext();
    return db.Products;
  }

}

The Select() method in Listing 1 returns all of the products from the Products database table. The method consists of two lines of code. The first line of code instantiates an instance of the StoreDataContext class. You created the StoreDataContext class when you created the LINQ to SQL entities. Next, the Products property of this class is used to return the products.

Notice that Listing 1 contains the definition for a class named Product and that this class is declared as a partial class. The other half of the partial class is contained in the Store.Designer.cs file that’s generated by the Object Relational Designer.

If you want your data access code to resemble SQL code more closely, you can use query syntax instead of method syntax. The class in Listing 2 does the same thing as the class in Listing 1. However, this new class uses query syntax instead of method syntax.

Listing 2 Product.cs (query syntax).

using System;
using System.Linq;
using System.Data.Linq;
using System.Collections.Generic;

public partial class Product
{

  public IEnumerable<Product> Select()
  {
    StoreDataContext db = new StoreDataContext();
    return from p in db.Products select p;
  }

}

The class in Listing 2 is very similar to the previous class. The only difference is that this new class uses the expression from p in db.Products select p to retrieve the products.

Whether you use method syntax or query syntax is entirely a matter of personal preference. There’s no performance difference between the two methods. If one type of syntax seems more natural to you, use it.

If you want to use either the class in Listing 1 or the class in Listing 2 to display the products in an ASP.NET page, you can use an ObjectDataSource control to represent the class. For example, the page in Listing 3 displays all of the products in a GridView control by binding the GridView to an ObjectDataSource control that represents the Product class.

Listing 3 ShowProducts.aspx.

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Show Products</title>
</head>
<body>
  <form id="form1" runat="server">

  <asp:GridView
    id="grd"
    DataSourceID="srcProducts"
    Runat="server" />

  <asp:ObjectDataSource
    id="srcProducts"
    TypeName="Product"
    SelectMethod="Select"
    Runat="server" />

  </form>
</body>
</html>

If you view the page in Listing 3 in a web browser, you’ll see the rendered content contained in Figure 5.

Figure 5

Figure 5 Displaying the contents of the Products database table.

It’s important to pause here for a moment in order to notice how simple LINQ to SQL makes accessing database data. You didn’t need to open a database connection or set up a command object. In fact, you didn’t write any ADO.NET or SQL code at all. LINQ to SQL reduced your data access code to its bare essentials. Think of how much time you could save by taking advantage of LINQ to SQL when writing a database-driven web application!

  • + Share This
  • 🔖 Save To Your Account