-
Notifications
You must be signed in to change notification settings - Fork 56
/
Program.cs
155 lines (135 loc) · 5.04 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace QueryExpressions
{
class Program
{
static void Main()
{
}
public static void QueryExpression()
{
IEnumerable<CultureInfo> commaCultures =
from culture in CultureInfo.GetCultures(CultureTypes.AllCultures)
where culture.NumberFormat.NumberDecimalSeparator == ","
select culture;
foreach (CultureInfo culture in commaCultures)
{
Console.WriteLine(culture.Name);
}
}
public static void WithoutLinq()
{
CultureInfo[] allCultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
foreach (CultureInfo culture in allCultures)
{
if (culture.NumberFormat.NumberDecimalSeparator == ",")
{
Console.WriteLine(culture.Name);
}
}
}
public static void ExtractingOneProperty()
{
IEnumerable<string> commaCultures =
from culture in CultureInfo.GetCultures(CultureTypes.AllCultures)
where culture.NumberFormat.NumberDecimalSeparator == ","
select culture.Name;
foreach (string cultureName in commaCultures)
{
Console.WriteLine(cultureName);
}
}
public static void EffectOfQueryExpression()
{
IEnumerable<string> commaCultures =
CultureInfo.GetCultures(CultureTypes.AllCultures)
.Where(culture => culture.NumberFormat.NumberDecimalSeparator == ",")
.Select(culture => culture.Name);
}
public static void ExpansionOfTrivialSelect()
{
IEnumerable<CultureInfo> commaCultures =
CultureInfo.GetCultures(CultureTypes.AllCultures)
.Where(culture => culture.NumberFormat.NumberDecimalSeparator == ",");
}
public static void QueryWithLetClause()
{
IEnumerable<string> commaCultures =
from culture in CultureInfo.GetCultures(CultureTypes.AllCultures)
let numFormat = culture.NumberFormat
where numFormat.NumberDecimalSeparator == ","
select culture.Name;
}
public static void ExpansionOfMultiVariableQueryExpression()
{
IEnumerable<string> commaCultures =
CultureInfo.GetCultures(CultureTypes.AllCultures)
.Select(culture => new { culture, numFormat = culture.NumberFormat })
.Where(vars => vars.numFormat.NumberDecimalSeparator == ",")
.Select(vars => vars.culture.Name);
}
public static void MeaninglessQuery()
{
var q = from x in new SillyLinqProvider()
where int.Parse(x)
select x.Hour;
}
public static void EffectOfMeaninglessQuery()
{
var q = new SillyLinqProvider().Where(x => int.Parse(x)).Select(x => x.Hour);
}
public static void AccidentalReevaluation()
{
var commaCultures =
from culture in CultureInfo.GetCultures(CultureTypes.AllCultures)
where culture.NumberFormat.NumberDecimalSeparator == ","
select culture;
object[] numbers = { 1, 100, 100.2, 10000.2 };
foreach (object number in numbers)
{
foreach (CultureInfo culture in commaCultures)
{
Console.WriteLine(string.Format(culture, "{0}: {1:c}",
culture.Name, number));
}
}
}
// The next examples illustrate types defined by .NET. Since .NET defines
// these, we do not want to define our own versions, hence the #if false
#if false
public interface IQueryable : IEnumerable
{
Type ElementType { get; }
Expression Expression { get; }
IQueryProvider Provider { get; }
}
public interface IQueryable<out T> : IEnumerable<T>, IQueryable
{
}
public interface IQueryProvider
{
IQueryable CreateQuery(Expression expression);
IQueryable<TElement> CreateQuery<TElement>(Expression expression);
object Execute(Expression expression);
TResult Execute<TResult>(Expression expression);
}
public static class Enumerable
{
public static IEnumerable<TSource> Where<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate)
...
}
public static class Queryable
{
public static IQueryable<TSource> Where<TSource>(
this IQueryable<TSource> source,
Expression<Func<TSource, bool>> predicate)
...
}
#endif
}
}