Advertisement
Caminhoneiro

linq REvaluation sync example

Jul 16th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6.  
  7. namespace Demos
  8. {
  9. //THIS WILL HAVE AN DELAY BETWEEN THE SYNC FROM THE FIRST AND THE SECOND
  10.     [TestClass]
  11.     public class LinqReEvaluationExample
  12.     {
  13.         [TestMethod]
  14.         public void WithReEvaluation()
  15.         {
  16.             var nums = new List<int> {1, 2, 3, 4};
  17.  
  18.             var q = from n in nums
  19.                     select new
  20.                                {
  21.                                    Number = n,
  22.                                    ExecutionTime = DateTime.Now.ToString("mm:ss:fff")
  23.                                };
  24.  
  25.             Debug.WriteLine("1st enumeration");
  26.             foreach (var n in q)
  27.             {
  28.                 Debug.WriteLine(n);
  29.             }
  30.  
  31.             Debug.WriteLine("2nd enumeration");
  32.             foreach (var n in q)
  33.             {
  34.                 Debug.WriteLine(n);
  35.             }            
  36.         }
  37.  
  38.  
  39. //THIS WILL NOT HAVE AN DELAY BETWEEN THE SYNC FROM THE FIRST AND THE SECOND BECAUSE OF ".ToList();" AT THE END
  40.         [TestMethod]
  41.         public void WithoutReEvaluation()
  42.         {
  43.             var nums = new List<int> { 1, 2, 3, 4 };
  44.  
  45.             var q = (from n in nums
  46.                      select new
  47.                                 {
  48.                                     Number = n,
  49.                                     ExecutionTime = DateTime.Now.ToString("mm:ss:fff")
  50.                                 }).ToList();
  51.  
  52.             Debug.WriteLine("1st enumeration");
  53.             foreach (var n in q)
  54.             {
  55.                 Debug.WriteLine(n);
  56.             }
  57.  
  58.             Debug.WriteLine("2nd enumeration");
  59.             foreach (var n in q)
  60.             {
  61.                 Debug.WriteLine(n);
  62.             }
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement