-
Notifications
You must be signed in to change notification settings - Fork 56
/
Projection.cs
132 lines (114 loc) · 4.25 KB
/
Projection.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
using System;
using System.Collections.Generic;
using System.Linq;
namespace StandardOperators
{
public static class Projection
{
public static void SelectWithIndex()
{
IEnumerable<string> nonIntro = Course.Catalog.Select((course, index) =>
$"Course {index}: {course.Title}");
}
public static void IndexedSelectDownstreamOfWhere()
{
IEnumerable<string> nonIntro = Course.Catalog
.Where(c => c.Number >= 200)
.Select((course, index) => $"Course {index}: {course.Title}");
}
public static void IndexedSelectUpstreamOfWhere()
{
IEnumerable<string> nonIntro = Course.Catalog
.Select((course, index) => new { course, index })
.Where(vars => vars.course.Number >= 200)
.Select(vars => $"Course {vars.index}: {vars.course.Title}");
}
public static void FetchingMoreThanNeeded()
{
using var dbCtx = new ExampleDbContext();
var pq = from product in dbCtx.Product
where product.ListPrice > 3000
select product;
foreach (var prod in pq)
{
Console.WriteLine($"{prod.Name} ({prod.Size}): {prod.ListPrice}");
}
}
public static void SelectClauseWithAnonymousType()
{
using var dbCtx = new ExampleDbContext();
var pq = from product in dbCtx.Product
where (product.ListPrice > 3000)
select new { product.Name, product.ListPrice, product.Size };
foreach (var prod in pq)
{
Console.WriteLine($"{prod.Name} ({prod.Size}): {prod.ListPrice}");
}
}
public static void SelectAsTransform()
{
int[] numbers = { 0, 1, 2, 3, 4, 5 };
IEnumerable<int> doubled = numbers.Select(x => 2 * x);
IEnumerable<int> squared = numbers.Select(x => x * x);
IEnumerable<string> numberText = numbers.Select(x => x.ToString());
}
public static void SelectManyQueryExpression()
{
int[] numbers = { 1, 2, 3, 4, 5 };
string[] letters = { "A", "B", "C" };
IEnumerable<string> combined = from number in numbers
from letter in letters
select letter + number;
foreach (string s in combined)
{
Console.WriteLine(s);
}
}
public static void SelectManyOperator()
{
int[] numbers = { 1, 2, 3, 4, 5 };
string[] letters = { "A", "B", "C" };
IEnumerable<string> combined = numbers.SelectMany(
number => letters,
(number, letter) => letter + number);
}
public static void SelectManyFlattenQueryExpression()
{
int[][] arrays =
{
new[] { 1, 2 },
new[] { 1, 2, 3, 4, 5, 6 },
new[] { 1, 2, 4 },
new[] { 1 },
new[] { 1, 2, 3, 4, 5 }
};
IEnumerable<int> combined = from row in arrays
from number in row
select number;
}
public static void SelectManyOperatorWithoutProjection()
{
int[][] arrays =
{
new[] { 1, 2 },
new[] { 1, 2, 3, 4, 5, 6 },
new[] { 1, 2, 4 },
new[] { 1 },
new[] { 1, 2, 3, 4, 5 }
};
var combined = arrays.SelectMany(row => row);
}
static IEnumerable<T2> MySelectMany<T, T2>(
this IEnumerable<T> src, Func<T, IEnumerable<T2>> getInner)
{
foreach (T itemFromOuterCollection in src)
{
IEnumerable<T2> innerCollection = getInner(itemFromOuterCollection);
foreach (T2 itemFromInnerCollection in innerCollection)
{
yield return itemFromInnerCollection;
}
}
}
}
}