Microsoft 9GD00001 Computer Accessories User Manual


 
194 Microsoft Visual Studio 2010: A Beginner’s Guide
options found after clicking the arrow button to expand Database Tools and then selecting
Table And Database Designers is “Prevent saving changes that require table re-creation.”
VS will not allow you to save a foreign key change to existing tables. However, by
unchecking “Prevent saving changes that require table re-creation,” you’ll be able to save
foreign key changes to an existing table.
As with so many other features of VS, there are literally dozens of database settings; most
are intuitive if you already understand SQL Server. Other options differ, depending on the
version of VS you have, and your Options screen might not look the same as Figure 7-10.
Now that you know how to create databases, tables, and stored procedures, you’ll need
to know how to use your database from code. The rest of this chapter shows you how to
use LINQ to work with data. First, we’ll look at the basic syntax of LINQ through LINQ
to Objects and then follow with working with SQL Server through LINQ to SQL.
Learning Language Integrated Query (LINQ)
LINQ is a set of features built into programming languages, such as C# and VB, for
working with data. It’s called Language Integrated Query because the LINQ syntax is
part of the language, as opposed to being a separate library. This section will show you
the essentials of LINQ with LINQ to Objects, a LINQ provider for querying in-memory
collections of objects. The great news is that the syntax you learn here is not only
applicable to LINQ to Objects, but to all other LINQ providers, such as LINQ to SQL and
more, that you’ll encounter.
The examples in this chapter will use a Console project for simplicity. Later chapters
will show you how to display data in desktop and Web applications. If you want to run the
code in this chapter, you can create a Console application and type the examples into the
Main method, as has been explained in each previous chapter of this book.
Querying Object Collections with LINQ
One way to use LINQ is via LINQ to Objects, which allows you to query collections
of objects. You can use LINQ to query any collection that implements the IEnumerable
interface. As you may recall, we discussed interfaces in Chapter 4; now you can see one
more example of how important interfaces are to .NET development. Listing 7-2 shows
a program that uses LINQ to query a collection. The object type is a custom class, named
Customer. The Main method creates a generic list of Customer and uses a LINQ query to
extract the Customer objects that have a first name that starts with the letter M.