EmailAddress = orlando0@adventure-works.com }, khách hàng mới { FirstName = "Keith", LastName = "Harris", EmailAddress = "keith0@adventure-works.com" }, khách hàng mới { FirstName = "Donna", LastName = "Carreras", EmailAddress = "donna0@adventure-works.com" }, khách hàng mới { FirstName = "Janet", LastName = "Cổng", EmailAddress = "janet1@adventure-works.com" }, khách hàng mới { FirstName = "Lucy", LastName = "Harrington", EmailAddress = "lucy0@adventure-works.com" } }; trở về khách hàng; } } }Đầu ra: FirstName == "Donna" Donna Carreras Email: donna0@adventure-works.com FirstName == "Donna"(đi 2) Donna Carreras Email: donna0@adventure-works.com Donna cổng Email: janet1@adventure-works.com Ví dụ 13-1 định nghĩa một lớp khách hàng đơn giản với ba đặc tính: FirstNameLastName, và e-mailAddress. Nó sẽ ghi đè các phương pháp Object.ToString() để cung cấp một chuỗi đại diện của trường hợp của nó.Tạo truy vấn các chương trình bắt đầu bằng cách tạo ra một danh sách khách hàng với một số dữ liệu mẫu, lợi dụng khởi tạo đối tượng như được thảo luận trong chương 4. Một khi danh sách khách hàng được tạo ra, ví dụ 13-1 định nghĩa một truy vấn LINQ:IEnumerable kết quả = từ khách hàng khách hàng nơi khách hàng. FirstName == "Donna" chọn khách hàng; Biến quả được khởi tạo với một biểu thức truy vấn. Trong ví dụ này, các truy vấn sẽ lấy tất cả các đối tượng khách hàng có tên là "Donna" từ danh sách khách hàng. Kết quả của một truy vấn là một bộ sưu tập thực hiện IEnumerable, where T is the type of the result object. In this example, because the query result is a set of Customer objects, the type of the result variable is IEnumerable. Let’s dissect the query and look at each part in more detail.The from clause The first part of a LINQ query is the from clause:from customer in customers The generator of a LINQ query specifies the data source and a range variable. A LINQ data source can be any collection that implements the System.Collections. Generic.IEnumerable interface. In this example, the data source is customers, an instance of List that implements IEnumerable.You’ll see how to do the same query against a SQL database in Chapter 15.A LINQ range variable is like an iteration variable in a foreach loop, iterating over the data source. Because the data source implements IEnumerable, the C# compiler can infer the type of the range variable from the data source. In this example, because the type of the data source is List, the range variable customer is of type Customer.Filtering The second part of this LINQ query is the where clause, which is also called a filter. This portion of the clause is optional:where customer.FirstName == "Donna"The filter is a Boolean expression. It is common to use the range variable in a where clause to filter the objects in the data source. Because customer in this example is of type Customer, you use one of its properties, in this case FirstName, to apply the filter for your query. Of course, you may use any Boolean expression as your filter. For instance, you can invoke the String.StartsWith() method to filter customers by the first letter of their last name:where customer.LastName.StartsWith("G") You can also use composite expressions to construct more complex queries. In addition, you can use nested queries where the result of one query (the inner query) is used to filter another query (the outer query).Projection (or select) The last part of a LINQ query is the select clause (known to database geeks as the “projection”), which defines (or projects) the results:select customer; In this example, the query returns the customer objects that satisfy the query condition. You may constrain which fields you project, much as you would with SQL. For instance, you can return only the qualified customers’ email addresses only: select customer.EmailAddress;Deferred Query Evaluation LINQ implements deferred query evaluation, meaning that the declaration and initialization of a query expression do not actually execute the query. Instead, a LINQ query is executed, or evaluated, when you iterate through the query result:foreach (Customer customer in result) Console.WriteLine(customer.ToString()); Because the query returns a collection of Customer objects, the iteration variable is an instance of the Customer class. You can use it as you would any Customer object. This example simply calls each Customer o
đang được dịch, vui lòng đợi..
