-
Notifications
You must be signed in to change notification settings - Fork 8
/
Program.cs
139 lines (113 loc) · 4.97 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.ComponentModel;
using System.Linq;
using linqshared;
namespace linq_partitioning
{
class Program: ProgramBase
{
static void Main(string[] args)
{
Linq20();
// Linq21();
// Linq22();
// Linq23();
// Linq24();
// Linq25();
// Linq26();
// Linq27();
}
[Category("Partitioning Operators")]
[Description("This sample uses a partition/slice to get only the first 3 elements of the array.")]
static void Linq20()
{
var numbers = new [] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var first3Numbers = numbers.Take(3);
Console.WriteLine("First 3 numbers:");
first3Numbers.ForEach(Console.WriteLine);
}
[Category("Partitioning Operators")]
[Description("This sample uses a partition/slice to get the first 3 orders from customers in Washington.")]
static void Linq21()
{
var customers = GetCustomerList();
var first3WAOrders = customers
.Where(c => c.Region == "WA")
.SelectMany(customer => customer.Orders, (customer, order) => new { customer, order })
.Select(x =>
new
{
x.customer.CustomerID,
x.order.OrderID,
x.order.OrderDate
})
.Take(3);
Console.WriteLine("First 3 orders in WA:");
first3WAOrders.ForEach(ObjectDumper.Write);
}
[Category("Partitioning Operators")]
[Description("This sample uses a partition to get all but the first four elements of the array.")]
static void Linq22()
{
var numbers = new []{ 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var allButFirst4Numbers = numbers.Skip(4);
Console.WriteLine("All but first 4 numbers:");
allButFirst4Numbers.ForEach(Console.WriteLine);
}
[Category("Partitioning Operators")]
[Description("This sample uses Take to get all but the first 2 orders from customers in Washington.")]
static void Linq23()
{
var customers = GetCustomerList();
var waOrders = customers
.Where(c => c.Region == "WA")
.SelectMany(customer => customer.Orders, (customer, order) => new { customer, order })
.Select(x =>
new
{
x.customer.CustomerID,
x.order.OrderID,
x.order.OrderDate
});
var allButFirst2Orders = waOrders.Skip(2);
Console.WriteLine("All but first 2 orders in WA:");
ObjectDumper.Write(allButFirst2Orders);
}
[Category("Partitioning Operators")]
[Description("This sample uses a partition to return elements starting from the beginning of the array until a number is read whose value is not less than 6.")]
static void Linq24()
{
var numbers = new[] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var firstNumbersLessThan6 = numbers.TakeWhile(n => n < 6);
Console.WriteLine("First numbers less than 6:");
firstNumbersLessThan6.ForEach(Console.WriteLine);
}
[Category("Partitioning Operators")]
[Description("This sample uses a partition to return elements starting from the beginning of the array until a number is hit that is less than its position in the array.")]
static void Linq25()
{
var numbers = new [] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var firstSmallNumbers = numbers.TakeWhile((n, index) => n >= index);
Console.WriteLine("First numbers not less than their position:");
firstSmallNumbers.ForEach(Console.WriteLine);
}
[Category("Partitioning Operators")]
[Description("This sample uses a partition to get the elements of the array starting from the first element divisible by 3.")]
static void Linq26()
{
var numbers = new [] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var allButFirst3Numbers = numbers.SkipWhile(n => n % 3 != 0);
Console.WriteLine("All elements starting from first element divisible by 3:");
allButFirst3Numbers.ForEach(Console.WriteLine);
}
[Category("Partitioning Operators")]
[Description("This sample uses a partition to get the elements of the array starting from the first element less than its position.")]
static void Linq27()
{
var numbers = new [] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var laterNumbers = numbers.SkipWhile((n, index) => n >= index);
Console.WriteLine("All elements starting from first element less than its position:");
laterNumbers.ForEach(Console.WriteLine);
}
}
}