Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
System.Linq.Expressions.Expression. Nói chung, bạn có thể sử dụng lambdas tuyên bố thay thế cho nhau với lambdas biểu. Tuy nhiên, bạn không thể chuyển đổi các lambdas tuyên bố thành cây biểu. Bạn có thể thể hiện cây biểu hiện bằng cách sử dụng cú pháp lambda biểu hiện. | 564 Chapter 15 Query Expressions Three things in the output are remarkable. First notice that after selection is assigned DelegateInvocations remains at zero. At the time of assignment to selection no iteration over Keywords is performed. If Keywords were a property the property call would run in other words the from clause executes at the time of assignment. However neither the projection the filtering nor anything after the from clause will execute until the code iterates over the values within selection. It is as though at the time of assignment selection would more appropriately be called query. However once we call Count a term such as selection or items that indicates a container or collection is appropriate because we begin to count the items within the collection. In other words the variable selection serves a dual purpose of saving the query information and acting like a container from which the data is retrieved. A second important characteristic to notice is that calling Count twice causes func to again be invoked once on each item selected. Since selection behaves both as a query and as a collection requesting the count requires that the query be executed again by iterating over the IEnumerable string collection selection refers to and counting the items returning the most up-to-date results. Similarly a foreach loop over selection would trigger func to be called again for each item. The same is true of all the other extension methods provided via System.Linq.Enumerable. Filtering In Listing 15.1 we include a where clause that filters out pure keywords but not contextual keywords. The where clause filters the collection vertically so that there are fewer items within the collection. The filter criteria are expressed with a predicate a lambda expression that returns a bool such as word.Contains as in Listing 15.1 or File.GetLastWrite-Time file DateTime.Now.AddMonths -1 as in Listing 15.6 the output of which appears in Output 15.5 . Listing 15.6 Anonymous