次の方法で共有


into (C# リファレンス)

intoコンテキスト キーワードを使用して、グループ結合、または 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($" {item.FirstLetter} has {item.Words} elements.");
        }
    }
}
/* Output:
   a has 2 elements.
   b has 2 elements.
*/

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

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

こちらも参照ください