LINQ to SQL

advertisement

Chapter 56: LINQ to SQL

What’s In This Chapter?

  • Working with LINQ to SQL using Visual Studio 2010

  • Mapping LINQ to SQL objects to database entities

  • Building LINQ to SQL operations without the O/R Designer

  • Using the O/R Designer with custom objects

  • Querying the SQL Server database using LINQ

  • Stored procedures and LINQ to SQL

You will probably find the .NET Language Integrated Query Framework (LINQ) in C# 2010 to be one of the more exciting features the language has to offer. Basically, what LINQ provides is a lightweight façade over programmatic data integration. This is such a big deal because data is king.

Pretty much every application deals with data in some manner, whether that data comes from memory (in-memory data), databases, XML files, text files, or something else. Many developers find it very difficult to move from the strongly typed object-oriented world of C# to the data tier where objects are second-class citizens. The transition from one world to the next was a kludge at best and was full of error-prone actions.

In C#, programming with objects means a wonderful, strongly typed ability to work with code. You can navigate very easily through the namespaces, work with a debugger in the Visual Studio IDE, and more. However, when you have to access data, you will notice that things are dramatically different.

You end up in a world that is not strongly typed, where debugging is a pain or even non-existent, and you end up spending most of the time sending strings to the database as commands. As a developer, you also have to be aware of the underlying data and how it is structured or how all the data points relate.

LINQ provides a strongly typed interface to the underlying data stores. It provides the means for developers to stay within the coding environment that they are used to and access the underlying data as objects that work with the IDE, IntelliSense, and even debugging.

With LINQ, the queries that you create now become first-class citizens within the .NET Framework alongside everything else you are used to. When you work with queries for your data store, you quickly realize that they now work and behave as if they are types in the system. This means that you can now use any .NET-compliant language and query the underlying data store as you never have before.

NOTE: Chapter 11, “Language Integrated Query,” provides an introduction to LINQ.

Figure 56-1 shows LINQ’s place in querying data.

Figure 56-1

Looking at the figure, you can see that there are different types of LINQ capabilities, depending on the underlying data that you are going to work with in your application. From the list, you find the following LINQ technologies:

  • LINQ to Objects

  • LINQ to DataSets

  • LINQ to SQL

  • LINQ to Entities

  • LINQ to XML

As a developer, you are given class libraries that provide objects that, using LINQ, can be queried as any other data store can. Objects are really nothing more than data that is stored in memory. In fact, your objects themselves might be querying data. This is where LINQ to Objects comes into play.

LINQ to SQL (the focus of this chapter), LINQ to Entities, and LINQ to DataSets provide the means to query relational data. Using LINQ, you can query directly against your database and even against the stored procedures that your database exposes. The last item from the diagram is the ability to query against your XML using LINQ to XML (this topic is covered in Chapter 33, “Manipulating XML”). The big thing that makes LINQ exciting is that it matters very little what you are querying against, because your queries will be quite similar.

This article is excerpted from chapter 56 "LINQ to SQL" of the book "Professional C# 4 and .NET 4" by Christian Nagel, Bill Evjen, Jay Glynn, Karli Watson, Morgan Skinner (ISBN: 978-0-470-50225-9, Wrox, 2010, Copyright Wiley Publishing Inc.)

Links: 
Tags: