Language integrated query
Số trang: 59
Loại file: pdf
Dung lượng: 1.83 MB
Lượt xem: 11
Lượt tải: 0
Xem trước 6 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
When you build a LINQ query expression and assign it to a query variable, very little code is executed in that statement. The data becomes available only when you iterate over that query variable, which executes the query once for each result in the result set. So, for example, if the result set consists of 100 items and you only iterate over the first 10, you don’t pay the price for computing the remaining 90 items in the result set unless you apply some sort of operator such as Average, which requires you to iterate over the entire collection.
Nội dung trích xuất từ tài liệu:
Language integrated query CHAPTER 16 ■ LINQ: LANGUAGE INTEGRATED QUERYusing System;using System.Linq;public class GroupExample{ static void Main() { int[] numbers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // Partition numbers into odd and // even numbers. var query = from x in numbers group x by x % 2 into partition where partition.Key == 0 select new { Key = partition.Key, Count = partition.Count(), Group = partition }; foreach( var item in query ) { Console.WriteLine( mod2 == {0}, item.Key ); Console.WriteLine( Count == {0}, item.Count ); foreach( var number in item.Group ) { Console.Write( {0}, , number ); } Console.WriteLine( \n ); } }} In this query, the continuation (the part of the query after the into clause) filters the series of groupswhere Key is 0 by using a where clause. This filters out the group of even numbers. I then project thatgroup out into an anonymous type, producing a count of items in the group to go along with the Keyproperty and the items in the group. Thus the output to the console includes only one group. But what if I wanted to add a count to each group in the partition? As I said before, the into clause isa generator. So I can produce the desired result by changing the query to this: var query = from x in numbers group x by x % 2 into partition select new { Key = partition.Key, Count = partition.Count(), Group = partition }; Notice that I removed the where clause, thus removing any filtering. When executed with thisversion of the query, the example produces the following desired output:mod2 == 0 561CHAPTER 16 ■ LINQ: LANGUAGE INTEGRATED QUERY Count == 5 0, 2, 4, 6, 8, mod2 == 1 Count == 5 1, 3, 5, 7, 9, In both of the previous query expressions, note that the result is not an IEnumerable as it commonly is when the group clause is the final projector. Rather, the end result is an IEnumerable where T is replaced with our anonymous type. The Virtues of Being Lazy When you build a LINQ query expression and assign it to a query variable, very little code is executed in that statement. The data becomes available only when you iterate over that query variable, which executes the query once for each result in the result set. So, for example, if the result set consists of 100 items and you only iterate over the first 10, you don’t pay the price for computing the remaining 90 items in the result set unless you apply some sort of operator such as Average, which requires you to iterate over the entire collection. ■ Note You can use the Take extension method, which produces a deferred execution enumerator, to access a specified number of elements at the head of the given stream. Similarly useful methods are TakeWhile, Skip, and SkipWhile. The benefits of this deferred execution approach are many. First of all, the operations described in the query expression could be quite expensive. Because those operations are provided by the user, and the designers of LINQ have no way of predicting the complexity of those operations, it’s best to harvest each item only when necessary. Also, the data could be in a database halfway around the world. You definitely want lazy evaluation on your side in that case. And finally, the range variable could actually iterate over an infinite sequence. I’ll show an example of that in the next section. C# Iterators Foster Laziness Internally, the query variable is implemented using C# iterators by using the yield keyword. I explained in Chapter 9 that code containing yield statements actually compiles into an iterator object. Therefore, when you assign the LINQ expression to the query variable, just about the only code that is executed is the constructor for the iterator object. The iterator might depend on other nested objects, and they are562 CHAPTER 16 ■ LINQ: LANGUAGE INTEGRATED QUERYinitialized as well. You get the results of the LINQ expression once you start iterating over the queryvariable using a foreach statement, or by using the IEnumerator interface. As an example, let’s have a look at a query slightly modified from the code in the earlier section“LINQ Query Expressions.” For convenience, here is the relevant code: var query = from employee in employees where employee.Salary > 100000 select new { LastName = employee.LastName, FirstName = employee.FirstName }; Console.WriteLine( Highly paid employees: ); foreach( var item in query ) { Console.WriteLine( {0}, {1}, item.LastName, item.FirstName ); Notice that the only difference is that I removed the orderby clause from the original LINQexpression; I’ll explain why in the next section. In this case, the query is translated into a series ofchained extension method calls on the employees variable. Each of those methods returns an object thatimplements IEnumerable. In reality, those objects ar ...
Nội dung trích xuất từ tài liệu:
Language integrated query CHAPTER 16 ■ LINQ: LANGUAGE INTEGRATED QUERYusing System;using System.Linq;public class GroupExample{ static void Main() { int[] numbers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // Partition numbers into odd and // even numbers. var query = from x in numbers group x by x % 2 into partition where partition.Key == 0 select new { Key = partition.Key, Count = partition.Count(), Group = partition }; foreach( var item in query ) { Console.WriteLine( mod2 == {0}, item.Key ); Console.WriteLine( Count == {0}, item.Count ); foreach( var number in item.Group ) { Console.Write( {0}, , number ); } Console.WriteLine( \n ); } }} In this query, the continuation (the part of the query after the into clause) filters the series of groupswhere Key is 0 by using a where clause. This filters out the group of even numbers. I then project thatgroup out into an anonymous type, producing a count of items in the group to go along with the Keyproperty and the items in the group. Thus the output to the console includes only one group. But what if I wanted to add a count to each group in the partition? As I said before, the into clause isa generator. So I can produce the desired result by changing the query to this: var query = from x in numbers group x by x % 2 into partition select new { Key = partition.Key, Count = partition.Count(), Group = partition }; Notice that I removed the where clause, thus removing any filtering. When executed with thisversion of the query, the example produces the following desired output:mod2 == 0 561CHAPTER 16 ■ LINQ: LANGUAGE INTEGRATED QUERY Count == 5 0, 2, 4, 6, 8, mod2 == 1 Count == 5 1, 3, 5, 7, 9, In both of the previous query expressions, note that the result is not an IEnumerable as it commonly is when the group clause is the final projector. Rather, the end result is an IEnumerable where T is replaced with our anonymous type. The Virtues of Being Lazy When you build a LINQ query expression and assign it to a query variable, very little code is executed in that statement. The data becomes available only when you iterate over that query variable, which executes the query once for each result in the result set. So, for example, if the result set consists of 100 items and you only iterate over the first 10, you don’t pay the price for computing the remaining 90 items in the result set unless you apply some sort of operator such as Average, which requires you to iterate over the entire collection. ■ Note You can use the Take extension method, which produces a deferred execution enumerator, to access a specified number of elements at the head of the given stream. Similarly useful methods are TakeWhile, Skip, and SkipWhile. The benefits of this deferred execution approach are many. First of all, the operations described in the query expression could be quite expensive. Because those operations are provided by the user, and the designers of LINQ have no way of predicting the complexity of those operations, it’s best to harvest each item only when necessary. Also, the data could be in a database halfway around the world. You definitely want lazy evaluation on your side in that case. And finally, the range variable could actually iterate over an infinite sequence. I’ll show an example of that in the next section. C# Iterators Foster Laziness Internally, the query variable is implemented using C# iterators by using the yield keyword. I explained in Chapter 9 that code containing yield statements actually compiles into an iterator object. Therefore, when you assign the LINQ expression to the query variable, just about the only code that is executed is the constructor for the iterator object. The iterator might depend on other nested objects, and they are562 CHAPTER 16 ■ LINQ: LANGUAGE INTEGRATED QUERYinitialized as well. You get the results of the LINQ expression once you start iterating over the queryvariable using a foreach statement, or by using the IEnumerator interface. As an example, let’s have a look at a query slightly modified from the code in the earlier section“LINQ Query Expressions.” For convenience, here is the relevant code: var query = from employee in employees where employee.Salary > 100000 select new { LastName = employee.LastName, FirstName = employee.FirstName }; Console.WriteLine( Highly paid employees: ); foreach( var item in query ) { Console.WriteLine( {0}, {1}, item.LastName, item.FirstName ); Notice that the only difference is that I removed the orderby clause from the original LINQexpression; I’ll explain why in the next section. In this case, the query is translated into a series ofchained extension method calls on the employees variable. Each of those methods returns an object thatimplements IEnumerable. In reality, those objects ar ...
Tìm kiếm theo từ khóa liên quan:
thủ thuật máy tính tài liệu công nghệ thông tin lập trình máy tính mẹo máy tính cài đặt máy tínhGợi ý tài liệu liên quan:
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 289 0 0 -
Làm việc với Read Only Domain Controllers
20 trang 269 0 0 -
Thêm chức năng hữu dụng cho menu chuột phải trên Windows
4 trang 249 0 0 -
70 trang 230 1 0
-
Bài giảng Tin học lớp 11 bài 1: Giới thiệu ngôn ngữ lập trình C#
15 trang 220 0 0 -
Tổng hợp lỗi Win 8 và cách sửa
3 trang 218 0 0 -
UltraISO chương trình ghi đĩa, tạo ổ đĩa ảo nhỏ gọn
10 trang 200 0 0 -
Giáo Trình tin học căn bản - ĐH Marketing
166 trang 195 0 0 -
15 trang 194 0 0
-
Sửa lỗi các chức năng quan trọng của Win với ReEnable 2.0 Portable Edition
5 trang 188 0 0