-
Notifications
You must be signed in to change notification settings - Fork 8
/
Program.cs
77 lines (63 loc) · 2.87 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Linq;
using System.ComponentModel;
using linqshared;
namespace linq_query
{
class Program : ProgramBase
{
static void Main(string[] args)
{
Linq99();
// Linq100();
// Linq101();
}
[Category("Query Execution")]
[Description("The following sample shows how query execution is deferred until the query is enumerated at a foreach statement.")]
static void Linq99()
{
// Queries are not executed until you enumerate over them.
var numbers = new[] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
int i = 0;
var simpleQuery = numbers
.Select(x => i++);
// The local variable 'i' is not incremented until the query is executed in the foreach loop.
Console.WriteLine($"The current value of i is {i}"); //i is still zero
simpleQuery.ForEach(item => Console.WriteLine($"v = {item}, i = {i}")); // now i is incremented
}
[Category("Query Execution")]
[Description("The following sample shows how queries can be executed immediately, and their results stored in memory, with methods such as ToList/list.")]
static void Linq100()
{
// Methods like ToList(), Max(), and Count() cause the query to be executed immediately.
var numbers = new[] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var i = 0;
var immediateQuery = numbers
.Select(x => ++i)
.ToList();
Console.WriteLine("The current value of i is {0}", i); //i has been incremented
immediateQuery.ForEach(item => Console.WriteLine($"v = {item}, i = {i}"));
}
[Category("Query Execution")]
[Description("The following sample shows how, because of deferred execution, queries can be used again after data changes and will then operate on the new data.")]
static void Linq101()
{
// Deferred execution lets us define a query once and then reuse it later in various ways.
var numbers = new[] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var lowNumbers = numbers
.Where(num => num <= 3);
Console.WriteLine("First run numbers <= 3:");
lowNumbers.ForEach(Console.WriteLine);
// Modify the source data.
for (var i = 0; i < 10; i++)
{
numbers[i] = -numbers[i];
}
// During this second run, the same query object,
// lowNumbers, will be iterating over the new state
// of numbers[], producing different results:
Console.WriteLine("Second run numbers <= 3:");
lowNumbers.ForEach(Console.WriteLine);
}
}
}