OrderByDescending(num => num).TakeWhile(num => num > 50);
foreach (int number in takeGreaterThan50)
listbox1.Items.Add(number);
When this query is run, the following numbers are returned:
100
94
86
81
77
70
66
65
55
This example could also be written using query syntax as follows:
IEnumerable
takeGreaterThan50 =
(from n in randomNumbers
order by n descending
select n).TakeWhile(num => num > 50);
As an interesting experiment, try modifying the original query as follows and executing it:
IEnumerable takeGreaterThan50 =
randomNumbers.OrderBy(num => num).TakeWhile(num =>
num > 50);
Was anything returned? Why not? As stated earlier, it returns elements based on a specified predicate
function, and continues to take the elements as long as the specified condition is true. If you order the
sequence in ascending order, the first element it finds does not meet the criteria and therefore the query
does not continue.
Putting Query Operators to Work
There was a ton of information in this chapter, so this section provides an example that enables you
to apply many of the operators you??™ve seen and to begin experimenting with the great functionality
provided by the LINQ query operators.
Pages:
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173