Article Copyright by d. United States. First Prev Next. Tuomas Hietanen. My vote of 1 www-sahilv 2-Dec Poor Description. Is the example correct? Parse s. ToArray ; foreach int num in nums Console. WriteLine num ; I got error at place where it is in bold. Error details Error 1 The name 's' does not exist in the current context Error 2 The type arguments for method 'System. Try specifying the type arguments explicitly. Good amount of info for a tip.
Would be helpful to include links for additional study. Put the shuffle code inside a loop, and stop when the sequence is back in its original order by applying the SequenceEquals method. You can see it would always be the final method in any query, because it returns a single value instead of a sequence:. Run the code you've got so far and take note of how the deck rearranges on each shuffle.
After 8 shuffles iterations of the do-while loop , the deck returns to the original configuration it was in when you first created it from the starting LINQ query.
The sample you've built so far executes an out shuffle , where the top and bottom cards stay the same on each run. Let's make one change: we'll use an in shuffle instead, where all 52 cards change position. For an in shuffle, you interleave the deck so that the first card in the bottom half becomes the first card in the deck.
That means the last card in the top half becomes the bottom card. This is a simple change to a singular line of code.
Update the current shuffle query by switching the positions of Take and Skip. This will change the order of the top and bottom halves of the deck:. Run the program again, and you'll see that it takes 52 iterations for the deck to reorder itself. You'll also start to notice some serious performance degradations as the program continues to run. There are a number of reasons for this. You can tackle one of the major causes of this performance drop: inefficient use of lazy evaluation.
Briefly, lazy evaluation states that the evaluation of a statement is not performed until its value is needed. LINQ queries are statements that are evaluated lazily. The sequences are generated only as the elements are requested.
Usually, that's a major benefit of LINQ. However, in a use such as this program, this causes exponential growth in execution time. Remember that we generated the original deck using a LINQ query. Each shuffle is generated by performing three LINQ queries on the previous deck. All these are performed lazily. That also means they are performed again each time the sequence is requested.
By the time you get to the 52nd iteration, you're regenerating the original deck many, many times. Let's write a log to demonstrate this behavior. Then, you'll fix it. In your Extensions. This extension method creates a new file called debug.
This extension method can be appended to any query to mark that the query executed. You will see a red squiggle under File , meaning it doesn't exist. It won't compile, since the compiler doesn't know what File is.
To solve this problem, make sure to add the following line of code under the very first line in Extensions. Notice that you don't log every time you access a query. You log only when you create the original query.
The program still takes a long time to run, but now you can see why. If you run out of patience running the in shuffle with logging turned on, switch back to the out shuffle. You'll still see the lazy evaluation effects. In one run, it executes queries, including all the value and suit generation.
You can improve the performance of the code here to reduce the number of executions you make. A simple fix you can make is to cache the results of the original LINQ query that constructs the deck of cards. Currently, you're executing the queries again and again every time the do-while loop goes through an iteration, re-constructing the deck of cards and reshuffling it every time. To cache the deck of cards, you can leverage the LINQ methods ToArray and ToList ; when you append them to the queries, they'll perform the same actions you've told them to, but now they'll store the results in an array or a list, depending on which method you choose to call.
Now the out shuffle is down to 30 queries. Run again with the in shuffle and you'll see similar improvements: it now executes queries. Please note that this example is designed to highlight the use cases where lazy evaluation can cause performance difficulties.
While it's important to see where lazy evaluation can impact code performance, it's equally important to understand that not all queries should run eagerly. The performance hit you incur without using ToArray is because each new arrangement of the deck of cards is built from the previous arrangement. IEnumerable and IQueryable in C. Differences between IEnumerable and IQueryable. LINQ Operators.
Linq Concat Method in C. Linq OrderBy Method in C. Linq Sum in C. Linq Max in C. Linq Min Method in C. Linq Average Method in C. Linq Count Method in C. Linq Aggregate Method in C. Linq All Operator in C. Linq Any in C.
Linq Contains in C. Linq GroupBy in C. Linq ToLookup Method in C. Linq Joins in C. Linq Inner Join in C. Linq Join with Multiple Data Sources.
0コメント