LINQ and lambdas - Get control of your data - a set of chained LINQ methods
Head First C Sharp: A Learner’s Guide to Real-World Programming with C Sharp and .NET (Andrew Stellman(著)、Jennifer Greene(著)、O’Reilly Media)の Chapter 9(LINQ and lambdas - Get control of your data)、p.515(mini exercise)の解答を求めてみる。
コード
Program.cs
int[] values = [0, 12, 44, 36, 92, 54, 13, 8];
p(values);
IEnumerable<int> result =
from v in values
where v < 37
orderby -v
select v;
p(result);
result = values.Where(v => v < 37).OrderBy(v => -v);
p(result);
static void p(IEnumerable<int> values)
{
Console.WriteLine("-----");
foreach (var item in values)
{
Console.WriteLine(item);
}
Console.WriteLine("-----");
}
入出力結果(Terminal, Zsh)
% dotnet run
-----
0
12
44
36
92
54
13
8
-----
-----
36
13
12
8
0
-----
-----
36
13
12
8
0
-----
%