into(C# 参考)into (C# Reference)

可使用 into 上下文关键字创建临时标识符,将 groupjoinselect 子句的结果存储至新标识符。The into contextual keyword can be used to create a temporary identifier to store the results of a group, join or select clause into a new identifier. 此标识符本身可以是附加查询命令的生成器。This identifier can itself be a generator for additional query commands. 有时称在 groupselect 子句中使用新标识符为“延续” 。When used in a group or select clause, the use of the new identifier is sometimes referred to as a continuation.

示例Example

下面的示例演示使用 into 关键字来启用具有推断类型 IGrouping 的临时标识符 fruitGroupThe following example shows the use of the into keyword to enable a temporary identifier fruitGroup which has an inferred type of IGrouping. 通过使用该标识符,可对每个组调用 Count 方法,并且仅选择那些包含两个或更多个单词的组。By using the identifier, you can invoke the Count method on each group and select only those groups that contain two or more words.

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);
        }

        // Keep the console window open in debug mode
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}
/* Output:
   a has 2 elements.
   b has 2 elements.
*/

仅当希望对每个组执行附加查询操作时,才需在 group 子句中使用 intoThe use of into in a group clause is only necessary when you want to perform additional query operations on each group. 有关详细信息,请参阅 group 子句For more information, see group clause.

有关在 join 子句中使用 into 的示例,请参见 join 子句For an example of the use of into in a join clause, see join clause.

请参阅See also