into (C# 参考)

使用into上下文关键字创建一个临时标识符,用于存储或joinselect子句的结果group。 此标识符可以充当其他查询命令的生成器。 在或select子句中使用新标识符group时,有时称为延续

C# 语言参考记录了 C# 语言的最新发布版本。 它还包含即将发布的语言版本公共预览版中功能的初始文档。

本文档标识了在语言的最后三个版本或当前公共预览版中首次引入的任何功能。

小窍门

若要查找 C# 中首次引入功能时,请参阅 有关 C# 语言版本历史记录的文章。

以下示例演示如何使用关键字创建名为 <a0/> 的临时标识符,该标识符的类型为推断。 使用此标识符,可以针对每个组调用 Count 该方法,并仅选择包含两个或多个单词的组。

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.
*/

仅当想要对每个组执行其他查询作时,才需要在 into 子句中使用 group 。 有关详细信息,请参阅 group 子句

有关在子句中使用join的示例into,请参阅 join 子句

另请参阅