計算機科学のブログ

LINQ and lambdas - Get Control of your data - query, from, in, orderby, select, where, var keyword

Head First C#: A Learner’s Guide to Real-World Programming with C# and .NET Core (Andrew Stellman(著)、Jennifer Greene(著)、O’Reilly Media)のChapter 9(LINQ and lambdas - Get Control of your data)、p.481(LINQ magnets)の解答を求めてみる。

コード

using System;
using System.Collections.Generic;
using System.Linq;

namespace MyFirstConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] badgers = { 36, 5, 91, 3, 41, 69, 8 };

            var skunks =
                from pigeon in badgers
                where (pigeon != 36 && pigeon < 50)
                orderby pigeon descending
                select pigeon + 5;
            var bears = skunks.Take(3);
            var weasels =
                from sparrow in bears
                select sparrow - 1;
            Console.WriteLine("Get your kicks on route {0}", weasels.Sum());
        }
    }
}

入出力結果(Terminal, Zsh)

Get your kicks on route 66