0%

CSharp Linq

Linqpad8 linq

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
//1.
var numbers = new List<int>();
numbers.Add (1);

IEnumerable<int> query = numbers.Select (n => n * 10); // Build query

numbers.Add (2); // Sneak in an extra element

query.Dump ("Notice both elements are returned in the result set");

numbers.Add (3);

query.Dump();//10,20,30

//2.
var numbers = new List<int>() { 1, 2 };

List<int> timesTen = numbers
.Select (n => n * 10)
.ToList(); // Executes immediately into a List<int>

numbers.Clear();
timesTen.Count.Dump ("Still two elements present");

//3.
int[] numbers = { 1, 2 };

int factor = 10;
IEnumerable<int> query = numbers.Select (n => n * factor);

factor = 20;

query.Dump ("Notice both numbers are multiplied by 20, not 10");