Databases Reference
In-Depth Information
How It Works
You declare a string array called names :
string[] names = {"James Huddleston", "Pearly", "Ami Knox", "Rupali Agarwal",
"Beth Christmas", "Fabio Claudio", "Vamika Agarwal", "Vidya Vrat Agarwal"};
In order to retrieve names from the string array, you query the string array using
IEnumerable<string> and also loop through the names array with the help of foreach using
the LINQ to Objects query syntax.
IEnumerable<string> namesOfPeople = from name in names
where name.Length <= 16
select name;
foreach (var name in namesOfPeople)
Using LINQ to SQL
LINQ to SQL is a facility for managing and accessing relational data as objects. It's logi-
cally similar to ADO.NET in some ways, but it views data from a more abstract perspec-
tive that simplifies many operations. It connects to a database, converts LINQ constructs
into SQL, submits the SQL, transforms results into objects, and even tracks changes and
automatically requests database updates.
A simple LINQ query requires three things:
• Entity classes
• A data context
• A LINQ query
Try It Out: Coding a Simple LINQ to SQL Query
In this exercise, you'll use LINQ to SQL to retrieve all customers from the Northwind Cus-
tomers table.
1. Navigate to Solution Explorer, right-click the Chapter19 solution, and select Add ä
New Project. From the provided list of Visual Studio installed templates, choose
Console Application and name the newly added project LinqToSql. Click OK.
2. Rename Program.cs to LinqToSql.cs . Replace the code in LinqToSql.cs with the
code in Listing 19-2.
Search WWH ::




Custom Search