tailieunhanh - Beginning C# 2008 Databases From Novice to Professional phần 10

Để lấy tên từ mảng chuỗi, bạn truy vấn các mảng chuỗi bằng cách sử dụng vòng lặp IEnumerable và cũng thông qua các mảng tên với sự giúp đỡ của foreach bằng cách sử dụng LINQ để truy vấn các đối tượng cú pháp. IEnumerable namesOfPeople = từ tên trong tên nơi | CHAPTER 19 USING LINQ 439 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 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 logically similar to in some ways but it views data from a more abstract perspective 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 Customers 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 to . Replace the code in with the code in Listing 19-2. 440 CHAPTER 19 USING LINQ Listing 19-2. using System using using using namespace Chapter19 Table public class Customers Column public string customerId Column public string companyName Column public string city Column public string country class LinqToSql static void Main string args connection string string connString @ server . sqlexpress integrated security true database northwind create data context DataContext db new .