into (C# リファレンス)

into コンテキスト キーワードは、groupjoin、または select 句の結果を新しい識別子に保存するための一時的な識別子を作成するために使用できます。 この識別子自体を追加のクエリ コマンドのジェネレーターにすることができます。 group または select 句で使用する場合、新しい識別子の使用は継続と呼ばれることもあります。

次の例では、into キーワードを使用して、推定の型 IGrouping を持つ一時的な識別子 fruitGroup を有効にする方法を示します。 識別子を使用すると、各グループに対して Count メソッドを呼び出し、2 つ以上の単語を含むグループのみを選択することができます。

class IntoSample1
{
    static void Main()
    {

        // Create a data source.
        string[] words = ["apples", "blueberries", "oranges", "bananas", "apricots"];

        // Create the query.
        var wordGroups1 =
            from w in words
            group w by w[0] into fruitGroup
            where fruitGroup.Count() >= 2
            select new { FirstLetter = fruitGroup.Key, Words = fruitGroup.Count() };

        // Execute the query. Note that we only iterate over the groups,
        // not the items in each group
        foreach (var item in wordGroups1)
        {
            Console.WriteLine(" {0} has {1} elements.", item.FirstLetter, item.Words);
        }
    }
}
/* Output:
   a has 2 elements.
   b has 2 elements.
*/

group 句での into の使用は、各グループに対して追加のクエリ操作を実行する場合にのみ必要です。 詳しくは、「group 句」をご覧ください。

join 句での into の使用例については、「join 句」を参照してください。

関連項目