LINQ to SQL C# Help

Probably the biggest and most exciting addition to the .NET Framework 3.5 is the addition of the .NET Language Integrated Query Framework (LINQ) into C# 2008.Basically, what LINQ provides is a lightweight facade 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 the 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.

Microsoft has provided LINQ as a lightweight facade that provides a strongly typed interface to the underlying data stores. LINQ provides the means for developers to stay within the coding environment 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 the data store you are working with, you will 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.

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

Figure 27-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 be working with in your application From the list, you will 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, 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.