select 句を重ねるには into を使う。または let 句を使う
Enumerable.Select メソッドを複数呼び出している式を クエリ式に変換する方法です。
- select 句の後ろに into を使う。するとクエリを継続できる。
- let 句を使う。
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { var xs = new[] { 1, 2, 3 }; // メソッドチェーン var a = xs.Select(x => x * 2).Select(y => y + 1); Console.WriteLine("a: " + string.Join(" ", a)); // クエリ式 var b = from x in xs select x * 2 into y // 結果を y に格納する select y + 1; Console.WriteLine("b: " + string.Join(" ", b)); // let 句を使う var c = from x in xs let y = x * 2 // 結果を y に格納する select y + 1; Console.WriteLine("c: " + string.Join(" ", c)); } }
実行結果です。
a: 3 5 7 b: 3 5 7 c: 3 5 7