into (C# 参考)

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

示例:

下面的示例演示如何使用into关键字启用具有推断类型的IGrouping临时标识符fruitGroup。 通过使用标识符,可以对每个组调用 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 子句

另请参阅